'Imports ServerControl.ServerTimer
Imports System.Threading
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System.Data
Imports System.Data.SqlClient
Imports System.Drawing.Imaging
Imports BarcodeLib.Barcode.Linear


Public Class frmMain

    Private MyReceiveStatusString As String = ""
    Private MyReceiveProcessString As String = ""
    Const MyCheckInterval As Integer = 1000 'Interval of  timer check listbox
    Const MyTimeOutValue As Integer = 1000 'Time-out  of listboxtimer  
    Const MyCheckConnectValue As Integer = 50 '5000'CircleTime  of listboxtimer  
    Const MyMultipleReleaseCart As Integer = 1800

    Private MyServerTimer As Timer
    Private MyInterval As Integer = 0

    Declare Function OpenIcon Lib "user32" (ByVal hwnd As Long) As Long
    Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long


    Function checkListTestStat(ByVal _custId As String, ByVal _partname As String) As Integer
        Dim cmd As New SqlCommand("SELECT count(customer) FROM dbo.ListStatTest WHERE partname='" & _partname.Trim & "' and customer = '" & _custId.Trim & "'", cn.OpenConnectionSQL("tvn_warehouse"))
        cmd.CommandType = CommandType.Text
        Dim dr As SqlDataReader
        dr = cmd.ExecuteReader
        While dr.Read
            Return dr(0)
        End While
        Return 0
    End Function

    Protected Overrides Sub OnHandleCreated(ByVal e As System.EventArgs)
        Dim CurrentProcesses() As Process
        CurrentProcesses = Process.GetProcessesByName _
                           (Process.GetCurrentProcess.ProcessName)
        '------------------------------------------------------------
        ' If there is no previous copy
        ' of the application running, bail
        '------------------------------------------------------------
        If CurrentProcesses.GetUpperBound(0) <= 0 Then
            Exit Sub
        Else
            MessageBox.Show("CHUONG TRINH DA DANG CHAY!")
        End If

        '------------------------------------------------------------
        ' Make the prior instance of the application
        ' the active window and terminate this.
        ' new instance
        '------------------------------------------------------------
        Dim i As Integer
        Dim Result As Long
        Dim ProcessHandle As Long
        For i = 0 To CurrentProcesses.GetUpperBound(0)
            ProcessHandle = CurrentProcesses(i).MainWindowHandle.ToInt32
            If ProcessHandle <> 0 Then
                '** Restore and activate the copy already running
                Result = OpenIcon(ProcessHandle)
                Result = SetForegroundWindow(ProcessHandle)
            End If
        Next
        End

    End Sub

    Private Sub ExitProgram()
        Me.Close()
        Me.Dispose()
        MyServerConnection = Nothing
        ErrRecording("btnExit_Click", "frmMain/btnExit_Click", "EXIT THE PROGRAM!")
    End Sub

    Private Sub StopP()
        Me.Invoke(New EventHandler(AddressOf StopPC))
    End Sub

    Private Sub StopPC(ByVal sender As Object, ByVal e As EventArgs)
        Me.Close()
        Me.Dispose()
        End
        MyServerConnection = Nothing
        ErrRecording("btnExit_Click", "frmMain/btnExit_Click", "EXIT THE PROGRAM!")
    End Sub

    Private Sub frmMain_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        MyServerConnection.Disposing()
    End Sub

    Private Sub frmMain_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        ' Me.Close()
        Me.Dispose()
        MyServerConnection = Nothing
        MySVRTVNWConnection = Nothing
        ErrRecording("btnExit_Click", "frmMain/btnExit_Click", "EXIT THE PROGRAM!")
        End
    End Sub

    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.CmdWO.Focus()
        Me.rbLOC.Checked = True
        MyTVNPartCode = "NONE"
        MyTVNPartCodeFlg = False
        SubWOFlg = False
        If MyDBConnection.DBConnecting() = False Then
            '  UpdateStatus("KHÔNG KẾT NỐI ĐƯỢC VỚI LOCAL !", "")
            MyOnlineFlg = False
            ErrRecording("", "frmMain/frmMain_Load", "Can not connect to SQL LOCAL ")
            MessageBox.Show("Khong ket noi voi database noi bo. Chuong trinh co the chay duoc. Bao cap tren")
        Else
            MyDBConnection.DBClosing()
            If MyServerConnection.ServerConnecting() = False Then
                '  UpdateStatus("KHÔNG KẾT NỐI ĐƯỢC VỚI SERVER !", "")
                MyOnlineFlg = False
                ErrRecording("", "frmMain/frmMain_Load", "Can not connect to SQL MyServerConnection ")
                MessageBox.Show("Khong ket noi voi database CONG TY MyServerConnection. Chuong trinh co the chay duoc. Bao cap tren")
            Else
                'UpdateStatus("BẮT ĐẦU THỰC HIỆN XỬ LÝ DỮ LIỆU ...", "")
                ErrRecording("", "frmMain/frmMain_Load", "START THE PROGRAM !")
                MyOnlineFlg = True
                ServerTimerEvent("ĐỌC KỆ CHỨA PART", "")
                MyStep = 1
                '  LoadDataToTemplate()
            End If
        End If
        If MySVRTVNWConnection.SVRTVNWConnecting() = False Then
            MyOnlineFlg = False
            ErrRecording("", "frmMain/frmMain_Load", "Can not connect to SQL MySVRTVNWConnection ")
            MessageBox.Show("Khong ket noi voi database CONG TY MySVRTVNWConnection . Chuong trinh co the chay duoc. Bao cap tren")
        Else
            'UpdateStatus("BẮT ĐẦU THỰC HIỆN XỬ LÝ DỮ LIỆU ...", "")
            ErrRecording("", "frmMain/frmMain_Load", "START THE PROGRAM !")
            MyOnlineFlg = True
            ServerTimerEvent("ĐỌC KỆ CHỨA PART", "")
            MyStep = 1
            '  LoadDataToTemplate()
        End If
        If CurrentCode = "10484" Then
            Me.DRInputToolStripMenuItem.Visible = True
        End If
        Dim ServerTimerDelegate As New TimerCallback(AddressOf ServerChangeListBox)
        MyServerTimer = New Timer(ServerTimerDelegate, Nothing, MyCheckInterval, MyTimeOutValue)
        If RMDP = True Then
            Me.DPTime.Enabled = True
            ' frmRMPanel.Show()
        Else
            Me.DPTime.Enabled = False
        End If
    End Sub

    Private Sub ExitP()
        Me.Close()
        Me.Dispose()
        MyServerConnection = Nothing
        ErrRecording("btnExit_Click", "frmMain/ExitP", "LOI LOI ")
    End Sub

    Private Sub ServerChangeListBox(ByVal stateInfo As Object)
        MyServerTimer.Change(Timeout.Infinite, Timeout.Infinite)
        If MyOnlineFlg = False Then

        End If
        While MyOnlineFlg = False
            MyOnlineFlg = CheckOnline()
            If MyOnlineFlg = True Then

                ErrRecording("", "frmMain/ServerChangeListBox", "A connection to SQL Server is open")
            End If
            Thread.Sleep(MyCheckConnectValue)
        End While
        Try
            If StopProgram = True Then
                StopP()
                Exit Sub
            End If

            If CompleteShelfFlg = True Then
                'If UpLoadToServer() = True Then
                CompleteShelfFlg = False
            End If
            'End If
        Catch ex As Exception

        Finally
            MyServerTimer.Change(MyCheckInterval, MyCheckInterval)
        End Try
    End Sub

    Private Function CheckShelfList() As Boolean
        Dim MyResult As Boolean = False
        Dim MyDataRow() As DataRow = Nothing
        Try
            MyDataRow = MyShelfList.Select("ID = '" & ReadingString & "'")
            If MyDataRow.Length <> 0 Then
                MyResult = True
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/CheckShelfList", ex.Message)
        End Try
        Return MyResult
    End Function

    Private Function CheckEmployeeList(ByVal MyCode As String) As Boolean
        Dim MyResult As Boolean = False
        Dim MyDataRow() As DataRow = Nothing
        Try
            MyDataRow = MyEmployeeList.Select("CodeID = '" & MyCode & "'")
            If MyDataRow.Length <> 0 Then
                MyOprName = MyDataRow(0)("FirstName") & " " & MyDataRow(0)("MidName") & " " & MyDataRow(0)("LastName")
                MyResult = True
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/CheckEmployeeList", ex.Message)
        End Try
        Return MyResult
    End Function

    Private Sub Timer_Control_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer_Control.Tick
        Dim MyString As String = ""
        Me.lblDateTime.Text = Format(Now, "dd-MMM-yy HH:mm:ss")
    End Sub

    Private Sub ServerTimerEvent(ByVal StatusMessage As String, ByVal ProcessMessage As String)
        MyReceiveStatusString = StatusMessage
        MyReceiveProcessString = ProcessMessage
        '  Invoke(New EventHandler(AddressOf ServerTimerUpdate))
    End Sub

    Private Sub ServerTimerCommand(ByVal StatusMessage As String, ByVal ProcessMessage As String)
        MyReceiveStatusString = StatusMessage
        MyReceiveProcessString = ProcessMessage
        'Invoke(New EventHandler(AddressOf ServerTimerUpdate))
    End Sub

    Public Function DateTranslating(ByVal MyDateStr As String) As String
        Const MyDaySymbolString As String = "123456789ABCDEFGHJKLMNPQRSTUVWX"
        Const MyMonthSymbolString As String = "123456789XYZ"
        Const MyDateString As String = "01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 "
        Dim MyDayStr As String = Mid(MyDateStr, 1, 2) & " "
        Dim MyDaySymbol As String = Mid(MyDaySymbolString, (InStr(MyDateString, MyDayStr, CompareMethod.Text) + 2) / 3, 1)
        Dim MyYearStr As String = Mid(MyDateStr, 7, 2)
        Dim MyMonthStr As String = Mid(MyDateStr, 4, 2) & " "
        Dim MyMonthSymbol As String = Mid(MyMonthSymbolString, (InStr(MyDateString, MyMonthStr, CompareMethod.Text) + 2) / 3, 1)
        Return MyYearStr & MyMonthSymbol & MyDaySymbol
    End Function

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        StopProgram = True
    End Sub

    Private Function UpdateData() As Boolean
        Dim MylastNoID As String = ""
        Dim MyResult As Boolean = False
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Dim MyID As String = ""
        Dim CheckDuplicateID As Boolean = False
        MyInputTime = Now()
        Dim CPN As String = "NONE"
        Dim MyDCode As String = Format(Now(), "MMyy")
        If MyCustPartName.Length > 0 Then
            CPN = MyCustPartName
        End If
        Try
            If MyServerConnection.ServerConnecting() = True Then
                If GetLastIDNo(MylastNoID, MyWO, "ParkingStock") = False Then
                    MessageBox.Show("Chương trình bị lỗi tìm LastNoID rồi bạn ơi ParkingStock")
                    'ElseIf GetLastIDNo(MylastNoID1, MyWO, "SubParkingStock") = False Then
                    '    MessageBox.Show("Chương trình bị lỗi tìm LastNoID rồi bạn ơi SubParkingStock")
                Else
                    For i As Integer = MylastNoID To MylastNoID + MyBQty
                        'Dim MyNo As String = Mid(MyStartID, 1, 4) & Mid(i, i.ToString.Length - 2, 3)
                        MyID = MyDCode & i.ToString("000") & "-" & MyWO
                        MySQLStr = "Select * from ParkingStock where ID ='" & MyID & "' "
                        ds = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                        If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count = 1 Then
                            MessageBox.Show("Chương trinh phát hiện trùng ID :" & MyID & " .ID này do nhân viên :" & ds.Tables(0).Rows(0)("Code") & "Thời gian :" & Format(ds.Tables(0).Rows(0)("inputtime"), "dd-MMM-yy hh:mm:ss"))
                            CheckDuplicateID = True
                        End If
                    Next
                    If CheckDuplicateID = False Then
                        MyResult = True
                        'If GetLastIDNo(MylastNoID, MyWO, "ParkingStock") = False Then
                        '    MessageBox.Show("Chương trình bị lỗi tìm LastNoID rồi bạn ơi ParkingStock")
                        '    'ElseIf GetLastIDNo(MylastNoID1, MyWO, "SubParkingStock") = False Then
                        '    '    MessageBox.Show("Chương trình bị lỗi tìm LastNoID rồi bạn ơi SubParkingStock")
                        'Else
                        For i As Integer = MylastNoID To MylastNoID + MyBQty
                            Dim MySW As String = MyWO & "_" & MyTVNPartCode & "_" & i.ToString("000")
                            Dim MyNo As String = MyDCode & i.ToString("000")
                            Dim MyStringQty As String = MyQty & "+" & MyExtraQty
                            MyID = MyDCode & i.ToString("000") & "-" & MyWO
                            MySQLStr = "INSERT INTO [ParkingStock]([ID],[WO],[NoID],[PartNumber],[Qty],[Customer],[RoHS],[Code],[InputTime],[CustomerPartName],[SubWO],[CartonSize])" _
                                & " VALUES ('" & MyID & "','" & MyWO & "','" & MyNo & "','" & MyPN & "','" & MyStringQty & "','" & MyCustInf & "','" & MyRoHSMarking & "','" & MyCode & "','" & MyInputTime & "','" & MyCustPartName & "','" & MySW & "','" & MyCartonSize & "')"
                            If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
                                MessageBox.Show("Dữ liệu bị lỗi " & MySQLStr)
                                MyResult = False
                            End If
                            If MyTVNPartCodeFlg = True Then
                                ' Dim MySubBox As Integer = Mid(MySubWO, MySubWO.Length - 1, 2)
                                Dim MyQty1 As String = ""
                                '  MyQty1 = Mid(MyQty, 1, InStr(MyQty, "+") - 1)
                                MyQty1 = CInt((MyQty / MySQty))
                                For j As Integer = 1 To MySQty
                                    Dim MySubID As String = MyID & "-" & j.ToString("00")
                                    'Dim MySubWOF As String = MyWO & "_" & Mid(MySubWO, 1, MySubWO.Length - 2) & j.ToString("00")
                                    Dim MySW1 As String = MyWO & "_" & MyTVNPartCode & "_" & i.ToString("000") & "_" & j.ToString("00")
                                    MySQLStr = "INSERT INTO [SubParkingStock]([ID],[WO],[NoID],[PartNumber],[Qty],[Customer],[RoHS],[Code],[InputTime],[CustomerPartName],[SubWO],[Comment],[CartonSize])" _
                                        & " VALUES ('" & MySubID & "','" & MyWO & "','" & MyNo & "','" & MyPN & "','" & MyQty1 & "','" & MyCustInf & "','" & MyRoHSMarking & "','" & MyCode & "','" & MyInputTime & "','" & MyCustPartName & "','" & MySW & "','" & MySW1 & "','" & MyCartonSize & "')"
                                    If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
                                        MessageBox.Show("Dữ liệu bị lỗi " & MySQLStr)
                                        MyResult = False
                                    End If
                                Next
                            End If
                            MyRPFlg = "WO"
                            MyRPID = MyWO
                        Next
                        DisplayDataToGrid()
                        If MyTVNPartCodeFlg = True Then
                            SubDisplayDataToGrid()
                        End If
                    End If
                End If
            Else
                MessageBox.Show("Chưa kết nối SQL server công ty")
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/UpLoadToServer", ex.Message)
        End Try
        Return MyResult
    End Function

    Private Function ConfirmQty(ByVal MyWO As String) As Boolean
        Dim MyResult As Boolean = False
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Dim MyID As String = ""
        Dim MyRecordQty As Integer = 0
        Try
            MySQLStr = "Select * from ParkingStock where [WO] = '" & MyWO & "' "
            DS = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
            If ds.Tables.Count <> 0 Then
                If ds.Tables(0).Rows.Count > 0 Then
                    For I As Integer = 0 To ds.Tables(0).Rows.Count - 1
                        MyRecordQty = MyRecordQty + CInt(Mid(ds.Tables(0).Rows(I)("Qty"), 1, InStr(ds.Tables(0).Rows(I)("Qty"), "+") - 1))
                    Next
                End If
                If CInt(MyWOQty) = (CInt(MyQty) * CInt(MyBQty) + CInt(MyRecordQty)) Then
                    MyResult = True
                ElseIf CInt(MyWOQty) < (CInt(MyQty) * CInt(MyBQty) + CInt(MyRecordQty)) Then
                    MessageBox.Show("SỐ LƯỢNG LỚN HƠN WO. TỔNG SỐ WO: " & MyWOQty & " .SỐ LƯỢNG YÊU CẦU:" & (CInt(MyQty) * CInt(MyBQty) + CInt(MyRecordQty)) & " .SỐ LƯỢNG ID ĐÃ NHẬP:" & MyRecordQty & " .SỐ LƯỢNG MỚI NHẬP :" & CInt(MyQty) * CInt(MyBQty) & " KIỂM TRA LẠI NHA")
                    MyResult = False
                ElseIf CInt(MyWOQty) > (CInt(MyQty) * CInt(MyBQty) + CInt(MyRecordQty)) Then
                    MessageBox.Show("SỐ LƯỢNG NHỎ HƠN WO. TỔNG SỐ WO: " & MyWOQty & " .SỐ LƯỢNG YÊU CẦU:" & (CInt(MyQty) * CInt(MyBQty) + CInt(MyRecordQty)) & " .SỐ LƯỢNG ID ĐÃ NHẬP:" & MyRecordQty & " .SỐ LƯỢNG MỚI NHẬP :" & CInt(MyQty) * CInt(MyBQty) & " TIẾP TỤC IN NHA")
                    MyResult = True
                Else
                    MessageBox.Show("LOI CHUONG TRÌNH RỒI" & CInt(MyWOQty) = (CInt(MyQty) * CInt(MyBQty) + CInt(MyRecordQty)))
                End If
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/ConfirmQty", ex.Message)
            MyResult = False
            MessageBox.Show("LOI CHUONG TRÌNH RỒI" & CInt(MyWOQty) = (CInt(MyQty) * CInt(MyBQty) + CInt(MyRecordQty)))
        End Try
        Return MyResult
    End Function

    Private Function GetLastIDNo(ByRef MyLastNoID As String, ByVal MyWO As String, ByVal MyTable As String) As Boolean
        Dim MyResult As Boolean = False
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        MyLastNoID = 0
        Try
            MySQLStr = "Select top 1 * from " & MyTable & " where WO ='" & MyWO & "' order by NoID desc"
            ds = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
            If ds.Tables.Count <> 0 Then
                MyResult = True
                If ds.Tables(0).Rows.Count = 1 Then
                    MyLastNoID = ds.Tables(0).Rows(0)("NoID")
                    MyLastNoID = Mid(MyLastNoID, 5, 3)
                Else
                    MyLastNoID = 0
                End If
                MyLastNoID = MyLastNoID + 1
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/GetLastIDNo", ex.Message)
        End Try
        Return MyResult
    End Function

    Private Function DisplayDataToGrid() As Boolean
        Dim MyResult As Boolean = False
        Dim MySQLStr As String = ""
        Dim DS As DataSet
        Dim MyID As String = ""
        Me.ViewData.Rows.Clear()
        Try
            If MyRPFlg = "WO" Then
                MySQLStr = "Select * from ParkingStock where [WO] = '" & MyRPID & "' "
            ElseIf MyRPFlg = "ID" Then
                MySQLStr = "Select * from ParkingStock where [ID] = '" & MyRPID & "' "
            End If
            DS = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
            If DS.Tables.Count <> 0 AndAlso DS.Tables(0).Rows.Count <> 0 Then
                MySQLStr = "DELETE FROM [PrintParkingStock]"
                MyServerConnection.ExecuteSQLSVR(MySQLStr)
                For i As Integer = 0 To DS.Tables(0).Rows.Count - 1
                    Me.ViewData.Rows.Add()
                    Me.ViewData.Rows(i).Cells("No").Value = i + 1
                    Me.ViewData.Rows(i).Cells("Customer").Value = DS.Tables(0).Rows(i)("Customer")
                    Me.ViewData.Rows(i).Cells("PartN").Value = DS.Tables(0).Rows(i)("PartNumber")
                    Me.ViewData.Rows(i).Cells("CustomerPartName").Value = DS.Tables(0).Rows(i)("CustomerPartName")
                    Me.ViewData.Rows(i).Cells("Wordorder").Value = DS.Tables(0).Rows(i)("WO")
                    Me.ViewData.Rows(i).Cells("OutQty").Value = DS.Tables(0).Rows(i)("Qty")
                    Me.ViewData.Rows(i).Cells("Number").Value = DS.Tables(0).Rows(i)("NoID")
                    Me.ViewData.Rows(i).Cells("InputTiming").Value = Format(DS.Tables(0).Rows(i)("InputTime"), "dd-MMM-yy hh:mm:ss")
                    Me.ViewData.Rows(i).Cells("OprCode").Value = DS.Tables(0).Rows(i)("Code")
                    Me.ViewData.Rows(i).Cells("Comment").Value = DS.Tables(0).Rows(i)("SubWO")
                    MySQLStr = "INSERT INTO [PrintParkingStock]([ID],[WO],[NoID],[PartNumber],[Qty],[Customer],[RoHS],[Code],[InputTime],[BarcodeLabel],[CustomerPartName],[SubWO],[Comment])" _
                        & " VALUES ('" & DS.Tables(0).Rows(i)("ID") & "','" & DS.Tables(0).Rows(i)("WO") & "','" & DS.Tables(0).Rows(i)("NoID") & "','" & DS.Tables(0).Rows(i)("PartNumber") & "','" & DS.Tables(0).Rows(i)("Qty") & "','" & DS.Tables(0).Rows(i)("Customer") & "','" & DS.Tables(0).Rows(i)("RoHS") & "','" & DS.Tables(0).Rows(i)("Code") & "','" & DS.Tables(0).Rows(i)("InputTime") & "','" & "*" & DS.Tables(0).Rows(i)("ID") & "*" & "','" & DS.Tables(0).Rows(i)("CustomerPartName") & "','" & DS.Tables(0).Rows(i)("Subwo") & "','" & DS.Tables(0).Rows(i)("Comment") & "')"
                    If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
                        MessageBox.Show("Dữ liệu bị lỗi " & MySQLStr)
                        MyResult = False
                    End If
                Next
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/DisplayToGrid", ex.Message)
        End Try
        Return MyResult
    End Function

    Private Function SubDisplayDataToGrid() As Boolean
        Dim MyResult As Boolean = False
        Dim MySQLStr As String = ""
        Dim DS As DataSet
        Dim MyID As String = ""
        Try
            If MyRPFlg = "WO" Then
                MySQLStr = "Select * from SubParkingStock where [WO] = '" & MyWO & "' "
            ElseIf MyRPFlg = "SID" Then
                MySQLStr = "Select * from ParkingStock where [ID] = '" & MyRPID & "' "
            End If
            DS = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
            If DS.Tables.Count <> 0 AndAlso DS.Tables(0).Rows.Count <> 0 Then
                MySQLStr = "DELETE FROM [SubPrintParkingStock]"
                MyServerConnection.ExecuteSQLSVR(MySQLStr)
                For i As Integer = 0 To DS.Tables(0).Rows.Count - 1
                    MySQLStr = "INSERT INTO [SubPrintParkingStock]([ID],[WO],[NoID],[PartNumber],[Qty],[Customer],[RoHS],[Code],[InputTime],[BarcodeLabel],[CustomerPartName],[SubWO],[Comment])" _
                        & " VALUES ('" & DS.Tables(0).Rows(i)("ID") & "','" & DS.Tables(0).Rows(i)("WO") & "','" & DS.Tables(0).Rows(i)("NoID") & "','" & DS.Tables(0).Rows(i)("PartNumber") & "','" & DS.Tables(0).Rows(i)("Qty") & "','" & DS.Tables(0).Rows(i)("Customer") & "','" & DS.Tables(0).Rows(i)("RoHS") & "','" & DS.Tables(0).Rows(i)("Code") & "','" & DS.Tables(0).Rows(i)("InputTime") & "','" & "*" & DS.Tables(0).Rows(i)("ID") & "*" & "','" & DS.Tables(0).Rows(i)("CustomerPartName") & "','" & DS.Tables(0).Rows(i)("Subwo") & "','" & DS.Tables(0).Rows(i)("Comment") & "')"
                    If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
                        MessageBox.Show("Dữ liệu bị lỗi " & MySQLStr)
                        MyResult = False
                    End If
                Next
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/DisplayToGrid", ex.Message)
        End Try
        Return MyResult
    End Function

    Private Sub CheckNR_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If MyRoHSMarking = "NRoHS" Then
            MyRoHSMarking = "RoHS"
        ElseIf MyRoHSMarking = "RoHS" Then
            MyRoHSMarking = "NRoHS"
        Else
            MyRoHSMarking = "RoHS"
        End If
    End Sub

    Private Sub CmdWO_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles CmdWO.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            If IsNumeric(CmdWO.Text) = True Then
                Me.lblTitle.BackColor = Color.Yellow
                MyDisplayFunctionFlg = "1"
                MyWO = UCase(Me.CmdWO.Text)
                LoadDataOfWO(MyWO)
                ' Me.CmdWO.Text = ""
                Me.CmdWO.Focus()
                '  BalUnitWoDelivery()
            Else
                MessageBox.Show("Yêu cầu nhập Số")
            End If
        End If
    End Sub

    Private Function LoadDataOfWO(ByVal MyWO As String) As Boolean
        Dim MyResult As Boolean = False
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Dim dscdn As DataSet
        Dim ds1 As DataSet
        Dim dsn As DataSet
        Dim dsL0 As DataSet
        Me.ViewData.Rows.Clear()
        Dim MyWIP As String = ""
        Dim MyOUTPUT As String = ""
        Dim MyIDCom As String = ""
        Dim MyCommnet As String = ""
        Dim MyPATime As String = ""
        Dim MyDPDQty As String = ""
        Dim MyPDRemainQty As String = ""
        Dim MyLevel As String = ""
        Dim ServerFlg As Boolean = False
        Dim APFlg As Boolean = False
        Dim MyOrgPartName As String = ""
        Dim QtyWO As Integer = 0
        Dim PClass As String = ""
        Dim Product As String = ""

        Me.ViewData.BackgroundColor = Color.Bisque
        Me.ViewData.Columns("No").HeaderText = "No"
        Me.ViewData.Columns("No").Width = 30
        Me.ViewData.Columns("Customer").HeaderText = "Customer"
        Me.ViewData.Columns("Customer").Width = 50
        Me.ViewData.Columns("PartN").HeaderText = "PartName"
        Me.ViewData.Columns("PartN").Width = 100
        Me.ViewData.Columns("Wordorder").HeaderText = "WO"
        Me.ViewData.Columns("Wordorder").Width = 50
        Me.ViewData.Columns("OutQty").HeaderText = "Qty"
        Me.ViewData.Columns("OutQty").Width = 60
        Me.ViewData.Columns("Delivery").HeaderText = "Delivery"
        Me.ViewData.Columns("Delivery").Width = 60
        Me.ViewData.Columns("WIP").HeaderText = "WHWIP"
        Me.ViewData.Columns("WIP").Width = 60
        Me.ViewData.Columns("BAL").HeaderText = "WH BAL"
        Me.ViewData.Columns("BAL").Width = 60
        Me.ViewData.Columns("PDQty").HeaderText = "PDQty"
        Me.ViewData.Columns("PDQty").Width = 60
        Me.ViewData.Columns("PDRemain").HeaderText = "PDRemain"
        Me.ViewData.Columns("PDRemain").Width = 60
        Me.ViewData.Columns("Comment").HeaderText = "Comment"
        Me.ViewData.Columns("Comment").Width = 120
        Me.ViewData.Columns("PartArriveTime").HeaderText = "PartArriveTime"
        Me.ViewData.Columns("PartArriveTime").Width = 100
        Me.ViewData.Columns("PDQty").Visible = True
        Me.ViewData.Columns("PDRemain").Visible = True

        Try
            If MyDBConnection.DBConnecting = True Then
                'MySQLStr = "SELECT matran10.wono as wono, matran10.custno as custno, matran10.item as item, Sum(matran10.oqty) AS SQTY, matran10.shipdte, matran10.level as l FROM matran10 GROUP BY matran10.wono, matran10.custno, matran10.item, matran10.shipdte, matran10.level HAVING (((matran10.wono)='" & MyWO & "') AND ((matran10.level)<>1)) ORDER BY matran10.item"
                MySQLStr = "SELECT matran10.wono as wono, matran10.custno as custno, matran10.item as item, sum(matran10.oqty) AS SQTY, matran10.shipdte FROM matran10 GROUP BY matran10.wono, matran10.custno, matran10.item, matran10.shipdte HAVING ((matran10.wono)='" & MyWO & "') ORDER BY matran10.item"
                'MySQLStr = "SELECT wono, custno, item, Sum(oqty) AS SQTY ,shipdte,level FROM matran10 GROUP BY wono, custno, item, shipdte, level  HAVING (((wono)='" & MyWO & "') and level <> '1') order by item"
                ds = MyDBConnection.ExecuteProcedureDBSVR(MySQLStr)
                If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                    MySQLStr = "SELECT * FROM matran10 where wono = '" & MyWO & "' and level = 0"
                    ds1 = MyDBConnection.ExecuteProcedureDBSVR(MySQLStr)
                    If ds1.Tables.Count <> 0 AndAlso ds1.Tables(0).Rows.Count > 0 Then
                        PClass = Trim(ds1.Tables(0).Rows(0)("category"))
                        Product = Trim(ds1.Tables(0).Rows(0)("item"))
                        MyShipDate = ds1.Tables(0).Rows(0)("shipdte")
                    Else
                        MessageBox.Show("Loi xac dinh P Class")
                    End If
                    If PClass Is DBNull.Value = True OrElse Len(PClass) <= 0 Then
                        MessageBox.Show("Khong tim dc truong du lieu BOM _PClass. Ban nhap lai thu coi")
                        Exit Function
                    End If
                    ServerFlg = False
                    APFlg = True
                    MySQLStr = "Select * from WH_MaterialInControl where CDNumber ='" & Product & "'  and StatusData ='RMD'"
                    dscdn = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
                    If dscdn.Tables.Count <> 0 AndAlso dscdn.Tables(0).Rows.Count = 1 Then
                        MessageBox.Show("WO này nói rằng :" & dscdn.Tables(0).Rows(0)("OffComment"))
                    End If
DisplatTempData:
                    For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                        'If Mid(ds.Tables(0).Rows(i)("item"), 1, 2) = "CD" Then
                        '    MyShipDate = ds.Tables(0).Rows(i)("shipdte")
                        'End If
                        MyLevel = "1"
                        MyCommnet = ""
                        MyPATime = ""
                        QtyWO = CInt(ds.Tables(0).Rows(i)("SQTY"))
                        'MySQLStr = "SELECT matran10.wono as wono,(matran10.oqty) AS SWQTY, matran10.level as l from matran10  where  ((matran10.wono)='" & MyWO & "' and matran10.level = '0') "
                        MySQLStr = "SELECT matran10.wono as wono, matran10.item as item ,Sum(matran10.oqty) AS SWQTY,  matran10.level as l FROM matran10 GROUP BY matran10.wono, matran10.custno, matran10.item, matran10.shipdte, matran10.level HAVING (((matran10.wono)='" & MyWO & "') AND ((matran10.level) = 0) and matran10.item = '" & ds.Tables(0).Rows(i)("item") & "') ORDER BY matran10.item"
                        dsL0 = MyDBConnection.ExecuteProcedureDBSVR(MySQLStr)
                        If dsL0.Tables.Count <> 0 AndAlso dsL0.Tables(0).Rows.Count > 0 Then
                            QtyWO = CInt(dsL0.Tables(0).Rows(0)("SWQTY"))
                            MyLevel = "0"
                        End If
                        MyIDCom = ds.Tables(0).Rows(i)("wono") & "-" & ds.Tables(0).Rows(i)("item")
                        MyWIP = GETWIP(ds.Tables(0).Rows(i)("item"))
                        MyOUTPUT = GETOUTPUT(ds.Tables(0).Rows(i)("item"))
                        MyPDRemainQty = GETPDREMAINQTY(ds.Tables(0).Rows(i)("item"))
                        MyDPDQty = GETDPDQTY(ds.Tables(0).Rows(i)("wono"), ds.Tables(0).Rows(i)("item"))
                        GETCommentData(MyIDCom, MyCommnet, MyPATime)
                        Me.ViewData.Rows.Add()
                        Me.ViewData.Rows(i).Cells("No").Value = i + 1
                        Me.ViewData.Rows(i).Cells("Customer").Value = ds.Tables(0).Rows(i)("custno") & IIf(PClass = "05", "-WH", "-TH")
                        Me.ViewData.Rows(i).Cells("PartN").Value = ds.Tables(0).Rows(i)("item")
                        Me.ViewData.Rows(i).Cells("Wordorder").Value = ds.Tables(0).Rows(i)("wono")
                        If ServerFlg = True AndAlso CInt(ds.Tables(0).Rows(i)("ColQTy")) > 1 Then
                            Me.ViewData.Rows(i).Cells("OutQty").Value = CInt(ds.Tables(0).Rows(i)("EQty1")) & IIf(CInt(ds.Tables(0).Rows(i)("EQty2")) = 0, "", "|" & CInt(ds.Tables(0).Rows(i)("EQty2"))) & IIf(CInt(ds.Tables(0).Rows(i)("EQty3")) = 0, "", "|" & CInt(ds.Tables(0).Rows(i)("EQty3"))) & IIf(CInt(ds.Tables(0).Rows(i)("EQty5")) = 0, "", "|" & CInt(ds.Tables(0).Rows(i)("EQty5"))) & IIf(CInt(ds.Tables(0).Rows(i)("EQty6")) = 0, "", "|" & CInt(ds.Tables(0).Rows(i)("EQty6"))) & IIf(CInt(ds.Tables(0).Rows(i)("EQty7")) = 0, "", "|" & CInt(ds.Tables(0).Rows(i)("EQty7"))) & IIf(CInt(ds.Tables(0).Rows(i)("EQty8")) = 0, "", "|" & CInt(ds.Tables(0).Rows(i)("EQty8"))) & IIf(CInt(ds.Tables(0).Rows(i)("EQty9")) = 0, "", "|" & CInt(ds.Tables(0).Rows(i)("EQty9"))) & IIf(CInt(ds.Tables(0).Rows(i)("EQty10")) = 0, "", "|" & CInt(ds.Tables(0).Rows(i)("EQty10")))
                            Me.ViewData.Rows(i).Cells("OutQty").Style.BackColor = Color.GreenYellow
                        Else
                            Me.ViewData.Rows(i).Cells("OutQty").Value = QtyWO 'CInt(ds.Tables(0).Rows(i)("SQTY"))
                            Me.ViewData.Rows(i).Cells("OutQty").Style.BackColor = Color.White
                        End If
                        Me.ViewData.Rows(i).Cells("WIP").Value = MyWIP
                        Me.ViewData.Rows(i).Cells("BAL").Value = CInt(MyWIP) - QtyWO 'CInt(ds.Tables(0).Rows(i)("SQTY"))
                        ' If CInt(MyWIP) - CInt(ds.Tables(0).Rows(i)("SQTY")) < 0 Then
                        If CInt(MyWIP) - QtyWO < 0 Then
                            Me.ViewData.Rows(i).Cells("BAL").Style.BackColor = Color.Blue
                        End If
                        Me.ViewData.Rows(i).Cells("Delivery").Value = MyOUTPUT
                        '  If CInt(MyOUTPUT) < CInt(ds.Tables(0).Rows(i)("SQTY")) Then
                        If CInt(MyOUTPUT) < QtyWO Then
                            Me.ViewData.Rows(i).Cells("Delivery").Style.BackColor = Color.Aquamarine
                        End If

                        Me.ViewData.Rows(i).Cells("PDQty").Value = MyDPDQty
                        Me.ViewData.Rows(i).Cells("PDRemain").Value = MyPDRemainQty

                        Me.ViewData.Rows(i).Cells("Comment").Value = MyCommnet
                        If MyPATime <> "" Then
                            Dim t2 As Date = MyPATime
                            Me.ViewData.Rows(i).Cells("PartArriveTime").Value = Format(t2, "dd-MMM-yy hh:mm:ss")
                        End If
                        'MySQLStr = "Select * from BOM where WONO = '" & ds.Tables(0).Rows(i)("wono") & "' and item ='" & ds.Tables(0).Rows(i)("item") & "' "
                        If CurrentCode = "10484" Then

                            Dim IDRD As String = Trim(ds.Tables(0).Rows(i)("wono") & "-" & ds.Tables(0).Rows(i)("item"))
                            MySQLStr = "Select * from BOM where ID ='" & IDRD & "' "
                            Dim dsb As DataSet = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
                            '=======Cập nhật cho IQC_TestStat========
                            'Write by: Hoan Dinh (23/09/2013)========
                            If checkListTestStat(ds.Tables(0).Rows(i)("custno"), ds.Tables(0).Rows(i)("item")) > 0 Then
                                Dim _qty As Double
                                _qty = drRev.fillDataset(cn.OpenConnectionSQL("systemcontrol"), "spCSO", "_qty", New String() {"@flag", "@WO", "@CD", "@custno"}, New Object() {5, ds.Tables(0).Rows(i)("wono").ToString.Trim, ds.Tables(0).Rows(i)("item").ToString.Trim, ds.Tables(0).Rows(i)("custno").ToString.Trim}).Tables(0).Rows(0).Item(0)
                                drRev.SP_Capnhat(cn.OpenConnectionSQL("tvn_warehouse"), "spBOM", New String() {"@flag", "@wo", "@CD", "@custId", "@qty"}, New Object() {4, ds.Tables(0).Rows(i)("wono").ToString.Trim, ds.Tables(0).Rows(i)("item").ToString.Trim, ds.Tables(0).Rows(i)("custno").ToString.Trim, IIf(IsDBNull(_qty), 0, _qty)})
                                cn.OpenConnectionSQL("tvn_warehouse").Close()
                            End If
                            '========================================
                            If dsb.Tables.Count <> 0 AndAlso dsb.Tables(0).Rows.Count = 0 Then
                                MyOrgPartName = Trim(ds.Tables(0).Rows(i)("item"))
                                If Mid(MyOrgPartName, MyOrgPartName.Length - 2, 3) = "-FT" Then
                                    MyOrgPartName = Mid(MyOrgPartName, 1, MyOrgPartName.Length - 3)
                                End If
                                MySQLStr = "INSERT INTO [BOM]([ID],[Commander],[wono],[custno],[item],[AccPacitem],[Shipdate],[SystemTime],[ItemLevel],[PClass],[oqty],[ColQty],[EQty1],[EQty2],[EQty3],[EQty4],[EQty5],[EQty6],[EQty7],[EQty8],[EQty9],[EQty10])" _
                                & " VALUES ('" & IDRD & "','" & CurrentCode & "','" & Trim(ds.Tables(0).Rows(i)("wono")) & "','" & Trim(ds.Tables(0).Rows(i)("custno")) & "','" & MyOrgPartName & "','" & Trim(ds.Tables(0).Rows(i)("item")) & "','" & MyShipDate & "','" & Now() & "','" & Trim(MyLevel) & "','" & PClass & "'," & QtyWO & ",1," & QtyWO & ",0,0,0,0,0,0,0,0,0)"
                                If APFlg = True AndAlso MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = False Then
                                    MessageBox.Show("chưa luu bom vao sql server")
                                End If
                            ElseIf dsb.Tables.Count <> 0 AndAlso dsb.Tables(0).Rows.Count = 1 Then
                                MySQLStr = "UPDATE BOM SET EQty" & dsb.Tables(0).Rows(0)("ColQty") + 1 & "= " & QtyWO & ", [ColQty] = '" & dsb.Tables(0).Rows(0)("ColQty") + 1 & "'" _
                                & "where ID= '" & IDRD & "' and  EQty" & dsb.Tables(0).Rows(0)("ColQty") & " <> " & QtyWO & ""
                                If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = False Then
                                    MessageBox.Show("chưa luu bom vao sql server")
                                End If
                                '======Cập nhật Pclass=========
                                MySQLStr = "UPDATE BOM SET PClass='" & PClass & "' where ID= '" & IDRD & "'"
                                If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = False Then
                                    MessageBox.Show("chưa cap nhat duoc pclass")
                                End If
                                '==============================
                                If (dsb.Tables(0).Rows(0)("Commander") Is DBNull.Value = True) OrElse (dsb.Tables(0).Rows(0)("Commander") <> CurrentCode) AndAlso CurrentCode = "10484" Then
                                    MySQLStr = "UPDATE BOM SET Commander ='" & CurrentCode & "',[SysTemTime] ='" & Now() & "',ItemLevel ='" & Trim(IIf(MyLevel Is DBNull.Value, " ", MyLevel)) & "' where ID ='" & IDRD & "' "
                                    If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = False Then
                                        MessageBox.Show("chưa luu doi comander . Bao cap tren")
                                    End If
                                End If
                            End If
                            'CheckPartOtherStockNo(ds.Tables(0).Rows(i)("item"))
                        End If
                    Next

                Else
                    MyResult = False
                    MessageBox.Show("WO NÀY TÌM ĐƯỢC DỮ LIỆU TRONG ACCPAC BẢNG MATRAN10. CHUONG TRING SU DUNG DU LIEU TEMP")
                    MySQLStr = "SELECT itemlevel as l,wono, custno, item, oqty as SQTY, ColQty,EQty1,EQty2,EQty3,EQty4,EQty5,EQty6,EQty7,EQty8,EQty9,EQty10 ,shipdate as shipdte FROM BOM where wono ='" & MyWO & "'  order by item"
                    ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
                    If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                        ServerFlg = True
                        APFlg = False
                        GoTo DisplatTempData
                    Else
                        MessageBox.Show("KO TIM THAY WO NAY O SERVER")
                    End If

                    'ErrRecording("", "frmMain/LoadDataOfWO", "WO NÀY TÌM ĐƯỢC DỮ LIỆU TRONG MATRAN10 " & MySQLStr)
                End If
            Else
                MyResult = False
                MessageBox.Show("KHÔNG KẾT NỐI SERVER ACCPAC CỤC BỘ.KIEM TRA LAI NHA")
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/LoadDataOfWO", ex.Message)
            MessageBox.Show("LOI. BAO CAP TREN")
        Finally
            If MyDBConnection IsNot Nothing Then
                MyDBConnection.DBClosing()
            End If
        End Try
        Return MyResult
    End Function

    Private Function GETPDREMAINQTY(ByVal MyP As String) As String
        Dim MyResult As String = 0
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Try
            MySQLStr = "SELECT PartNo, Sum(ActWIP) AS PDQTY FROM PDRemainQty GROUP BY PartNo HAVING (((PartNo)='" & Trim(MyP) & "'))"
            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count = 1 Then
                MyResult = ds.Tables(0).Rows(0)("PDQty")
            End If

        Catch ex As Exception
            ErrRecording("", "frmMain/GETPDREMAINQTY", ex.Message)
            MyResult = 0
        Finally
        End Try
        Return MyResult
    End Function

    Private Function GETDPDQTY(ByVal MyWO As String, ByVal MyP As String) As String
        Dim MyResult As String = 0
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Try
            MySQLStr = "SELECT PartNo, Sum(UnitDelivery) AS PDQTY FROM DeliveryWPD GROUP BY wo, PartNo HAVING WO='" & Trim(MyWO) & "' and PartNo='" & Trim(MyP) & "'"
            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count = 1 Then
                MyResult = ds.Tables(0).Rows(0)("PDQTY")
            End If

        Catch ex As Exception
            ErrRecording("", "frmMain/GETPDREMAINQTY", ex.Message)
            MyResult = 0
        Finally
        End Try
        Return MyResult
    End Function


    Private Function GETWIP(ByVal MyP As String) As String
        Dim MyResult As String = 0
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Try
            MySQLStr = "SELECT PartNo, Sum(Inventory) AS SQTY FROM tbInventory GROUP BY PartNo HAVING (((PartNo)='" & Trim(MyP) & "'))"
            ds = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count = 1 Then
                MyResult = ds.Tables(0).Rows(0)("SQty")
            End If

        Catch ex As Exception
            ErrRecording("", "frmMain/LoadDataOfWO", ex.Message)
            MyResult = 0
        Finally
        End Try
        Return MyResult
    End Function

    Private Function CheckPartOtherStockNo(ByVal MyP As String) As Boolean
        Dim MyResult As Boolean = False
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Dim Q As Integer = 0
        Try
            MySQLStr = "SELECT StockNo,PartNo, Sum(Inventory) AS SQTY FROM tbInventory GROUP BY PartNo,StockNo HAVING PartNo ='" & Trim(MyP) & "'  AND Sum(Inventory) > 0 AND  ( StockNo <> '" & StockNo & "' AND StockNo <> 'WH_QA') "
            ds = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                    Q = Q + ds.Tables(0).Rows(0)("SQTY")
                Next
                MessageBox.Show("Part nay " & Trim(MyP) & " Ton kho " & ds.Tables(0).Rows(0)("StockNo") & " la " & Q & " . Kiem tra lai nhe ban")
                MyResult = True
            Else
                MyResult = False
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/CheckPartOtherStockNo", ex.Message)
            MyResult = 0
        Finally
        End Try
        Return MyResult
    End Function


    Private Function GETCommentData(ByVal MyIDCom As String, ByRef MyComment As String, ByRef MyPATime As String) As String
        Dim MyResult As String = 0
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Try
            MySQLStr = "SELECT * FROM ShortageMaterial WHERE ID = '" & Trim(MyIDCom) & "'"
            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count = 1 Then
                MyComment = IIf(ds.Tables(0).Rows(0)("Comment") Is DBNull.Value, "", ds.Tables(0).Rows(0)("Comment"))
                MyPATime = IIf(ds.Tables(0).Rows(0)("PartArrivingTime") Is DBNull.Value, "", ds.Tables(0).Rows(0)("PartArrivingTime")) 'ds.Tables(0).Rows(0)("PartArrivingTime")
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/GETCommentData", ex.Message)
            MyResult = 0
        Finally
        End Try
        Return MyResult
    End Function

    Private Function GETOUTPUT(ByVal MyP As String) As String
        Dim MyResult As String = 0
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Try
            MySQLStr = " SELECT Sum(UnitDelivery) AS SD FROM Delivery_sp GROUP BY Workorder, PartNo HAVING Workorder = '" & MyWO & "'  AND PartNo='" & Trim(MyP) & "'"

            ds = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count = 1 Then
                MyResult = ds.Tables(0).Rows(0)("SD")
            End If

        Catch ex As Exception
            ErrRecording("", "frmMain/GETOUTPUT", ex.Message)
            MyResult = 0
        Finally
        End Try
        Return MyResult
    End Function

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        StopP()
    End Sub

    Private Sub ViewData_CellContentDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles ViewData.CellContentDoubleClick

        Try
            Me.ViewData.Rows(e.RowIndex).Cells("PDQty").Style.BackColor = Color.White
            SHTPartName = Trim(ViewData.Rows(e.RowIndex).Cells(2).Value)
            SHTWO = Trim(ViewData.Rows(e.RowIndex).Cells(3).Value)
            SHTQTY = Trim(ViewData.Rows(e.RowIndex).Cells(4).Value)
            SHTDQTY = Trim(ViewData.Rows(e.RowIndex).Cells(5).Value)
            SHTWHWIP = Trim(ViewData.Rows(e.RowIndex).Cells(6).Value)
            If (e.ColumnIndex = 10) Or (e.ColumnIndex = 11) AndAlso MyDisplayFunctionFlg = "1" Then
                SHTComment = Trim(ViewData.Rows(e.RowIndex).Cells(10).Value)
                SHTPATime = Trim(ViewData.Rows(e.RowIndex).Cells(11).Value)
                If UpdateShortageMaterial() = False Then
                    ErrRecording("", "frmMain/ViewData_CellContentDoubleClick", "Ko luu dc nha")
                    MessageBox.Show("Không lưu dc dữ liệu")
                Else
                    MessageBox.Show("OK")
                End If
            ElseIf (e.ColumnIndex = 5) Then
                If CheckCode(CurrentCode, "Admin", Name) = True And RDST.Checked = True Then
                    UpadteSTLoc(SHTDQTY, Trim(ViewData.Rows(e.RowIndex).Cells(1).Value))
                    Me.ViewData.Rows(e.RowIndex).Cells("Delivery").Style.BackColor = Color.Violet
                End If
            ElseIf (e.ColumnIndex = 1) Then
                Dim Name As String = ""
                ClickLOC = Trim(ViewData.Rows(e.RowIndex).Cells(5).Value)
                If CheckCodeControler(CurrentCode, "Admin", Name) = True Then
                    SHTID = Trim(ViewData.Rows(e.RowIndex).Cells(1).Value)
                    UpdateMoveMaterial()
                    Me.ViewData.Rows(e.RowIndex).Cells("No").Style.BackColor = Color.Violet
                End If
            ElseIf (e.ColumnIndex = 9) AndAlso MyDisplayFunctionFlg = "1" Then
                SHTComment = Trim(ViewData.Rows(e.RowIndex).Cells(10).Value)
                SHTPATime = Trim(ViewData.Rows(e.RowIndex).Cells(11).Value)
                Me.ViewData.Rows(e.RowIndex).Cells("PDQty").Style.BackColor = Color.Violet
                Dim PDQty As String = Trim(ViewData.Rows(e.RowIndex).Cells(8).Value)
                Dim MyPDRemain As String = Trim(ViewData.Rows(e.RowIndex).Cells(9).Value)
                If CInt(SHTDQTY) >= CInt(SHTQTY) Then
                    MessageBox.Show("PHÁT PART " & SHTPartName & ".SL :" & SHTDQTY & " ĐÃ ĐỦ WO RỒI BẠN ƠI")
                ElseIf CInt(SHTDQTY) + CInt(PDQty) >= CInt(SHTQTY) Then
                    MessageBox.Show("PART " & SHTPartName & ".SL KHO:" & SHTDQTY & ".SL PD:" & CInt(MyPDRemain) & " ĐÃ ĐỦ WO RỒI BẠN ƠI")
                ElseIf CInt(MyPDRemain) = 0 Then
                    MessageBox.Show("PART " & SHTPartName & " PD HẾT RỒI")
                    'ElseIf CInt(SHTDQTY) + CInt(MyPDRemain) < CInt(SHTQTY) Then
                    '    MessageBox.Show("PART " & SHTPartName & ".SL KHO:" & SHTDQTY & ".SL PD:" & CInt(MyPDRemain) & " ĐÃ ĐỦ WO RỒI BẠN ƠI")
                Else
                    UpdateWIPPDQty(PDQty, MyPDRemain)
                End If
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/ViewData_CellContentDoubleClick", ex.Message)
        End Try
    End Sub

    Private Function UpadteSTLoc(ByVal L As String, ByVal ID As String) As Boolean
        Dim Myresult As Boolean = False
        Dim MySQLStr As String = ""
        Try
            MySQLStr = "UPDATE IDandLocation SET Location ='" & L & "'where ID ='" & ID & "'"
            If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
                MessageBox.Show("chua Cap IDandLocation . tam ngung di")
                Exit Function
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/UpadteSTLoc", ex.Message)
            Myresult = False
        End Try
        Return Myresult
    End Function

    Private Function UpdateMoveMaterial()
        Dim Myresult As Boolean = False
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Dim MyID As String = Format(Now(), "yyMMddhh")
        'Dim MyDNo As String = "DRP" & Mid(MyID, 1, 6)
        Dim DQty As Integer = 0
        MyID = SHTID & MyID
        Dim MMNumber As String = ""
        Try
            MySQLStr = "SELECT * FROM WH_MovingMaterial where ID ='" & MyID & "'"
            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count = 0 Then
                MySQLStr = "INSERT INTO [WH_MovingMaterial]([ID],[IDRecord],[PartName],[Qty],[Shelf],[WHRec],[WHDel],[Code],[InputTime],[PrintStatus])" _
                & " VALUES ('" & MyID & "','" & SHTID & "','" & SHTPartName & "'," & SHTQTY & " ,'" & ClickLOC & "','" & MyWHRec & "','" & MyWHDel & "','" & CurrentCode & "','" & Now() & "','1')"
                If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = False Then
                    MessageBox.Show("KHÔNG LƯU DỮ LIỆU")
                End If
            Else
                ErrRecording("", "frmMain/UpdateWIPPDQty", "nhap roi" & MySQLStr)
                MessageBox.Show("Cái này nhập rồi bạn ơi ")
            End If

        Catch ex As Exception
            ErrRecording("", "frmMain/UpdateMoveMaterial", ex.Message)
            Myresult = False
        End Try
        Return Myresult
    End Function

    Private Function UpdateWIPPDQty(ByVal PDQty As Integer, ByVal MyPDRemain As Integer) As Boolean
        Dim Myresult As Boolean = False
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Dim MyID As String = Format(Now(), "yyMMddhhmmss")
        Dim MyDNo As String = "DRP" & Mid(MyID, 1, 6)
        Dim DQty As Integer = 0
        Dim DNQty As Integer = SHTQTY - (SHTDQTY + PDQty)
        Dim NewPDRemain As Integer = 0
        If MyPDRemain > DNQty Then
            NewPDRemain = MyPDRemain - DNQty
            DQty = DNQty
        ElseIf MyPDRemain <= DNQty Then
            NewPDRemain = 0
            DQty = MyPDRemain
        End If

        Try
            MySQLStr = "SELECT * FROM DeliveryWPD where ID ='" & MyID & "'"
            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count = 0 Then
                MySQLStr = "INSERT INTO [DeliveryWPD]([ID],[WO],[Deliverynumber],[PartNo],[UnitWO],[UnitDelivery],[DeliveryTime],[PrintTimes])" _
                & " VALUES ('" & MyID & "','" & SHTWO & "','" & MyDNo & "','" & SHTPartName & "' ," & SHTQTY & "," & DQty & ",'" & Now() & "',0)"
                If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = True Then
                    Myresult = True
                End If
            Else
                ErrRecording("", "frmMain/UpdateWIPPDQty", "nhap roi" & MySQLStr)
                MessageBox.Show("Cái này nhập rồi bạn ơi ")
            End If
            Dim ID As String = SHTWO & "- " & SHTPartName
            MySQLStr = "SELECT * FROM PDRemainQty where ID ='" & ID & "'"
            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count = 0 Then
                MySQLStr = "INSERT INTO [PDRemainQty]([ID],[StockNo],[PartNo],[WO],[WQty],[Delivery],[ActWIP],[UpdateTime])" _
                & " VALUES ('" & ID & "','" & StockNo & "','" & SHTPartName & "','" & SHTWO & "'," & SHTQTY & "," & DQty & ",'" & -DQty & "','" & Now() & "')"
                If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = True Then
                    Myresult = True
                End If
            Else
                ErrRecording("", "frmMain/UpdateWIPPDQty", "nhap roi . hqh nen xem lai" & MySQLStr)
                MessageBox.Show("Cái này nhập rồi bạn ơi ")
            End If

        Catch ex As Exception
            ErrRecording("", "frmMain/UpdateWIPPDQty", ex.Message)
            Myresult = False
        End Try
        Return Myresult
    End Function

    Private Function UpdateShortageMaterial() As Boolean
        Dim Myresult As Boolean = False
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Dim MyID As String = SHTWO & "-" & SHTPartName
        Dim MyReportTime As Date = Now()
        Dim MyPATime As Date
        Try
            MySQLStr = "SELECT * FROM ShortageMaterial where ID ='" & myid & "'"
            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count = 0 Then
                If SHTPATime = "" Then
                    MySQLStr = "INSERT INTO [ShortageMaterial]([ID],[PartName],[WO],[Qty],[DQty],[WHWip],[ReportTime],[ShipDate],[Comment])" _
                    & " VALUES ('" & MyID & "','" & SHTPartName & "','" & SHTWO & "'," & SHTQTY & " ," & SHTDQTY & ",'" & SHTWHWIP & "','" & MyReportTime & "','" & MyShipDate & "','" & SHTComment & "')"
                Else
                    'MyPATime = Format(SHTPATime, "dd-MMM-yy hh:mm:ss")
                    'objdate = Date.Parse(strDate).ToShortDateString

                    MyPATime = Date.Parse(SHTPATime).ToShortDateString
                    MySQLStr = "INSERT INTO [ShortageMaterial]([ID],[PartName],[WO],[Qty],[DQty],[WHWip],[ReportTime],[ShipDate],[Comment],[PartArrivingTime])" _
                    & " VALUES ('" & MyID & "','" & SHTPartName & "','" & SHTWO & "'," & SHTQTY & " ," & SHTDQTY & ",'" & SHTWHWIP & "','" & MyReportTime & "','" & MyShipDate & "','" & SHTComment & "','" & SHTPATime & "')"
                End If
                If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = True Then
                    Myresult = True
                End If
            Else
                ErrRecording("", "frmMain/UpdateShortageMaterial", "nhap roi" & MySQLStr)
                MessageBox.Show("Cái này nhập rồi bạn ơi " & ds.Tables(0).Rows(0)("PartName") & "  " & ds.Tables(0).Rows(0)("WO") & "  " & ds.Tables(0).Rows(0)("ReportTime"))
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/UpdateShortageMaterial", ex.Message)
            MessageBox.Show("KO CAP NHAT DC DU LIEU")
            Myresult = False
        End Try
        Return Myresult
    End Function

    Private Sub ShortageMaterialToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ShortageMaterialToolStripMenuItem.Click
        MyDisplayFunctionFlg = "5"
        Me.lblTitle.BackColor = Color.Yellow
        If LoadShortageMaterial() = False Then
            MessageBox.Show("Không hiển thị được. Báo cấp trên đi")
        End If
    End Sub

    Private Function LoadShortageMaterial() As Boolean
        Dim MyResult As Boolean = False
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Dim MyWIP As String = ""
        Me.ViewData.Rows.Clear()
        Me.ViewData.BackgroundColor = Color.Aqua

        Try
            MySQLStr = "SELECT * from ShortageMaterial where FinishFlg is null order by ReportTime "
            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                Me.ViewData.Columns("BAL").HeaderText = "Report Time"
                Me.ViewData.Columns("Customer").HeaderText = "Ship Date"
                Me.ViewData.Columns("WIP").HeaderText = "CurWIP/WHWIP"
                Me.ViewData.Columns("OutQty").HeaderText = "Qty"
                Me.ViewData.Columns("PDQty").Visible = False
                Me.ViewData.Columns("PDRemain").Visible = False
                For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                    MyWIP = GETWIP(ds.Tables(0).Rows(i)("PartName"))
                    Me.ViewData.Rows.Add()
                    If ds.Tables(0).Rows(i)("Priority") Is DBNull.Value = True Then

                    ElseIf ds.Tables(0).Rows(i)("Priority") = "1" Then
                        Me.ViewData.Rows(i).Cells("no").Style.BackColor = Color.Red
                    End If
                    Me.ViewData.Rows(i).Cells("No").Value = i + 1
                    Me.ViewData.Rows(i).Cells("Customer").Value = Format(ds.Tables(0).Rows(i)("ShipDate"), "dd-MMM-yy")
                    Me.ViewData.Rows(i).Cells("PartN").Value = ds.Tables(0).Rows(i)("PartName")
                    Me.ViewData.Rows(i).Cells("Wordorder").Value = ds.Tables(0).Rows(i)("wo")
                    Me.ViewData.Rows(i).Cells("OutQty").Value = CInt(ds.Tables(0).Rows(i)("QTY"))
                    Me.ViewData.Rows(i).Cells("WIP").Value = ds.Tables(0).Rows(i)("WHWIP") & "/" & MyWIP
                    Me.ViewData.Rows(i).Cells("Delivery").Value = ds.Tables(0).Rows(i)("DQTY")
                    Me.ViewData.Rows(i).Cells("BAL").Value = Format(ds.Tables(0).Rows(i)("ReportTime"), "dd-MMM-yy hh:mm:ss")
                    Me.ViewData.Rows(i).Cells("Comment").Value = IIf(ds.Tables(0).Rows(i)("Comment") Is DBNull.Value, "", ds.Tables(0).Rows(i)("Comment"))
                    Dim PRT As String = IIf(ds.Tables(0).Rows(i)("PartArrivingTime") Is DBNull.Value, "", ds.Tables(0).Rows(i)("PartArrivingTime"))
                    If PRT <> "" Then
                        Dim t1 As Date = PRT
                        Me.ViewData.Rows(i).Cells("PartArriveTime").Value = Format(t1, "dd-MMM-yy hh:mm:ss")
                    End If
                    MyResult = True
                Next
            Else
                MyResult = False
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/LoadShortageMaterial", ex.Message)
            MyResult = False
        End Try
        Return MyResult
    End Function

    Private Sub PrintStockDataToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MyPartLOC = Me.CmdLOC.Text
        If MyPartLOC = "" Then
            MessageBox.Show("Yêu cầu nhập LOCATION")
            Me.CmdLOC.Focus()
        Else
            If LoadWHData() = False Then
                MessageBox.Show("Loi. Khong co du lieu")
            End If
        End If
    End Sub

    Private Sub StockTakingToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MyPartLOC = Me.CmdLOC.Text
        If MyPartLOC = "" Then
            MessageBox.Show("Yêu cầu nhập LOCATION")
            Me.CmdLOC.Focus()
        End If
    End Sub
    Private Function LoadWHData() As Boolean

    End Function

    Private Sub CmdLOC_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles CmdLOC.KeyPress
        Me.lblTitle.BackColor = Color.Yellow
        Dim MyDeliveryNote As String = ""
        Dim QtyRMM As String = ""
        Dim QtyPicking As String = ""
        Dim RMMNote As String = Trim(CmdLOC.Text)
        If e.KeyChar = ChrW(Keys.Enter) Then
            'Me.Spart.Checked = False
            'If Me.RSN.Checked = True Then
            '    If SkidNo(RMMNote) = True Then
            '        frmSkidNo.Show()
            '    Else
            '        MessageBox.Show("Khong co du lieu cua chung tu nay. Bao cap tren")
            '    End If
            '    Exit Sub
            If Me.RdPW.Checked = True Then
                SearchPartOfWO(RMMNote)
            ElseIf Me.rdcpn.Checked = True Then
                If CreatePalletNo(RMMNote) = True Then
                    frmPallet.Show()
                End If
            ElseIf Me.rdRMM.Checked = True Then
                If CheckQtyRMMandPicking(RMMNote, QtyRMM, QtyPicking) = True Then
                    If UpdateDeliveryNoteWH2(RMMNote) = True Then
                        frmDeliveryMNote.Show()
                    End If
                Else
                    MessageBox.Show("SỐ LƯƠNG YÊU CẦU LÀ:" & QtyPicking & ", SỐ LƯƠNG ĐÃ LẤY LÀ:" & QtyRMM & ".CHƯA ĐỦ ĐÂU BẠN")
                End If
            ElseIf Me.RdDeliveryNote.Checked = True Then
                If VDeliveryMPDNote(Trim(Me.CmdLOC.Text)) = False Then
                    MessageBox.Show("KO CÓ PHIẾU XUẤT KHO. HOẶC CHƯA CẬP NHẬT IN PHIẾU XUẤT KHO")
                Else
                    frmDeliveryMNote.Show()
                End If
            ElseIf Me.rdIDD.Checked = True Then
                Dim MyName As String = ""
                If CheckCode(CurrentCode, "LeadEmployee", MyName) = True Then
                    If IDDeliveryMPD(Trim(Me.CmdLOC.Text)) = True Then
                        frmIDDelivery.Show()
                    Else
                        MessageBox.Show("KO IN DUOC NHAN")
                    End If
                Else
                    MessageBox.Show("CHI CO TO TRUONG MOI IN LAI NHAN DC")
                End If
            ElseIf Me.rbARM.Checked = True Then
                If RMLabel(UCase(Trim(Me.CmdLOC.Text))) = True Then
                    frmRML.Show()
                End If
            ElseIf Me.rdRRM.Checked = True Then
                If RRMNote(UCase(Trim(Me.CmdLOC.Text))) = True Then
                    frmReceipteMNote.Show()
                End If
            ElseIf Me.RdDNote.Checked = True Then
                MyGetPartNote = UCase(Trim(Me.CmdLOC.Text))
                'Me.Button2.Focus()
                Me.CmdLOC.Text = ""
                If MyGetPartNote = "" Then
                    MessageBox.Show("Yêu câu nhập phiếu lấy Part")
                Else
                    Dim RQtyFlg As Boolean = False
                    dsObj.MainTable.Clear()
                    dsSubObj.MainTable.Clear()
                    If DeliverymaterialPD(RQtyFlg) = True Then
                        frmIDDelivery.Show()
                        If RQtyFlg = True Then
                            MessageBox.Show("CÓ NHÃN TỒN CỦA NGUYÊN VẬT LIỆU. CHÚ Ý, CHÚ Ý NHA")
                            frmIDRemain.Show()
                        End If
                    Else
                        MessageBox.Show("KHÔNG CÓ DỮ LIỆU IN ĐÂU. XEM LẠI BẠN NHẬP CHƯA HAY IN ROI CHƯA")
                    End If
                End If

            ElseIf UCase(Mid((CmdLOC.Text), 1, 3)) = "DRP" Then
                MyGetPartNote = ""
                MyDeliveryNote = (Me.CmdLOC.Text)
                If LoadDeliveryNote(MyDeliveryNote) = False Then
                    MessageBox.Show("KHONG LOAD DC PHIEU XUAT KHO AO")
                End If
            Else
                MyGetPartNote = ""
                MyPartLOC = Trim(Me.CmdLOC.Text)
                If Me.RdPPC.Checked = True Then
                    MyDisplayFunctionFlg = "8"
                    'LoadPartAtPC()
                    ' PartDeliveryProduction()
                ElseIf Me.RDST.Checked = True Then
                    MyDisplayFunctionFlg = "2"
                    LoadStockTakingPartLocation()
                Else
                    LoadPartLocation() 'rbpart : kiểm tra tồn kho theo part
                End If
            End If
        End If
    End Sub

    Private Function RMLabel(ByVal ARM As String) As Boolean
        Dim MyResult As Boolean = False
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Dim ds4 As DataSet
        Dim MyPDes As String = ""
        Dim MyIDR As String = ""
        Dim MyIDBarcode As String = ""
        Dim MyPUnit As String = ""
        Dim ReceiptNumber As String = ""
        Dim NoR As Integer = 0
        dsObj.MainTable.Clear()
        Dim NN As Integer = 0
        Try
            MySQLStr = "Select * from WH_ReceiptMaterial where Marking ='1' and AReceiptNumber ='" & ARM & "' ORDER BY ARMTime"
            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)

            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                If CurrentCode = ds.Tables(0).Rows(0)("Code") Then
                    For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                        NoR = CInt(ds.Tables(0).Rows(i)("Markinglabel"))
                        For j As Integer = 0 To NoR - 1
                            NN = NN + 1
                            MyIDBarcode = Format(Now(), "yyMMddhhmmss") & NN.ToString("00")
                            MySQLStr = "INSERT INTO [BarcodePrinter]([IDbarcode],[PartNo],[PONumber],[Quantity],[TotalQty],[ReceiptDate],[ModifiedDate],[RMNote],[StockNu],[Location])" _
                            & " VALUES ('" & MyIDBarcode & "','" & ds.Tables(0).Rows(i)("PartNo") & "','" & ds.Tables(0).Rows(i)("PO") & "'," & ds.Tables(0).Rows(i)("QtyPerBox") & ", " & ds.Tables(0).Rows(i)("QtyPO") & ",'" & Format(ds.Tables(0).Rows(i)("ARMTime"), "dd-MMM-yy") & "','" & Now() & "','" & ARM & "','WH_T','" & ds.Tables(0).Rows(i)("Shelf") & "')"
                            If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
                                MessageBox.Show("loi roi " & MySQLStr)
                            Else
                                Threading.Thread.Sleep(500)
Cont1:
                                MySQLStr = "Select * from BarcodePrinter where IDbarcode = '" & MyIDBarcode & "' and RMNote = '" & ARM & "'"
                                ds4 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                                If ds4.Tables.Count <> 0 AndAlso ds4.Tables(0).Rows.Count = 1 Then
                                    MyIDR = ds4.Tables(0).Rows(0)("IDRecord")
                                    MySQLStr = "UPDATE WH_ReceiptMaterial SET MarkingLabel  = MarkingLabel - 1 where ID ='" & ds.Tables(0).Rows(i)("ID") & "' "
                                    If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = False Then
                                        MessageBox.Show("loi roi " & MySQLStr)
                                    Else
                                        MyResult = True
                                        dsObj.MainTable.AddMainTableRow(Trim(MyIDR), "*ID" & Trim(MyIDR) & "*", Trim(ds.Tables(0).Rows(i)("PartNo")), "*" & Trim(ds.Tables(0).Rows(i)("PartNo")) & "*", Trim(ds.Tables(0).Rows(i)("Shelf")), CInt(ds.Tables(0).Rows(i)("QtyPerBox")), ds.Tables(0).Rows(i)("QtyPerBox"), Trim(ds.Tables(0).Rows(i)("PO")), "*" & Trim(ds.Tables(0).Rows(i)("PO")) & "*", Format(ds.Tables(0).Rows(i)("ARMTime"), "MM-dd-yy"), ds.Tables(0).Rows(i)("CQtyPO") & "/" & ds.Tables(0).Rows(i)("QtyPO"), Trim(ds.Tables(0).Rows(i)("Code")), ds.Tables(0).Rows(i)("Vender"), "")
                                        GARM = ARM
                                    End If
                                    'End If
                                Else
                                    Dim MyMessage As String = ("Nhan Ok de tiep tuc in nhan. ")
                                    Dim Result As String = MessageBox.Show(MyMessage, "Hi", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
                                    If Result = Windows.Forms.DialogResult.OK Then
                                        GoTo Cont1
                                    ElseIf Result = Windows.Forms.DialogResult.Cancel Then
                                        MessageBox.Show("Ban da bo qua 1 nhan barcode")
                                    End If
                                End If

                            End If
                        Next
                        MySQLStr = "UPDATE WH_ReceiptMaterial SET Marking  = '3' where ID ='" & ds.Tables(0).Rows(i)("ID") & "' and MarkingLabel = 0"
                        If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = False Then
                            MessageBox.Show("loi roi " & MySQLStr)
                        End If
                    Next
                Else
                    MessageBox.Show("Bạn không được phân công nhập NVL")
                End If
            Else
                MySQLStr = "Select * from WH_ReceiptMaterial where Marking = '3' and MarkingLabel = '0'  and AReceiptNumber = '" & ARM & "' and (PBMarking is null or PBMarking ='1')"
                ds4 = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
                If ds4.Tables.Count <> 0 AndAlso ds4.Tables(0).Rows.Count > 0 Then
                    If CurrentCode = ds4.Tables(0).Rows(0)("Code") Then
                        'MySQLStr = "SELECT BarcodePrinter.Location as Location ,TotalQty as QtyPO, BarcodePrinter.IDRecord as IDRecord, tbInventory.StockNo, tbInventory.PartNo as PartNo, tbInventory.Receipt as QtyPerBox, BarcodePrinter.PONumber as PO, BarcodePrinter.ReceiptDate as ARMTime, RMNote FROM tbInventory INNER JOIN BarcodePrinter ON tbInventory.IDRecord = BarcodePrinter.IDRecord where RMNote = '" & ARM & "'"
                        MySQLStr = "SELECT *  FROM BarcodePrinter where RMNote = '" & ARM & "'"
                        ds = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                        If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                            For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                                MyResult = True
                                dsObj.MainTable.AddMainTableRow(ds.Tables(0).Rows(i)("IDRecord"), "*ID" & ds.Tables(0).Rows(i)("IDRecord") & "*", Trim(ds.Tables(0).Rows(i)("PartNo")), "*" & Trim(ds.Tables(0).Rows(i)("PartNo")) & "*", Trim(ds.Tables(0).Rows(i)("Location")), CInt(ds.Tables(0).Rows(i)("Quantity")), ds.Tables(0).Rows(i)("Quantity"), Trim(ds.Tables(0).Rows(i)("PONumber")), "*" & Trim(ds.Tables(0).Rows(i)("PONumber")) & "*", Format(ds4.Tables(0).Rows(0)("ARMTime"), "MM-dd-yy"), ds.Tables(0).Rows(i)("CQtyPO") & "/" & ds.Tables(0).Rows(i)("TotalQty"), CurrentCode, "Thermtrol", "")
                                GARM = ARM
                            Next
                        End If
                    Else
                        MessageBox.Show("Bạn không được phân công nhập NVL")
                    End If
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString())
            ErrRecording("", "frmMain/RMLabel", ex.Message)
            MyResult = False
        End Try
        Return MyResult
    End Function


    Private Function UpdateDeliveryNoteWH2(ByVal RMMNote As String) As Boolean
        Dim MyResult As Boolean = False
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Dim ds1 As DataSet
        Dim ds4 As DataSet
        Dim MyPDes As String = ""
        Dim MyPUnit As String = ""
        Dim ReceiptNumber As String = ""
        ' Dim StockNo As String = "WH_02"
        Try
            MySQLStr = "Select * from WH_MaterialTransfer where Marking ='1' and RMMNote = '" & RMMNote & "' "
            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                ReceiptNumber = "RMS " & Format(Now(), "yyMM")
                MySQLStr = "Select * from Delivery where substring(DeliveryNumber,1,8) = '" & ReceiptNumber & "' order by DeliveryNumber desc"
                ds4 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                If ds4.Tables.Count <> 0 AndAlso ds4.Tables(0).Rows.Count > 0 Then
                    ReceiptNumber = ReceiptNumber & " " & (CInt(Mid(ds4.Tables(0).Rows(0)("DeliveryNumber"), 10, 3)) + 1).ToString("000")
                Else
                    ReceiptNumber = ReceiptNumber & " 001"
                End If
                MySQLStr = "INSERT INTO [Delivery]([TransactionNumber],[DeliveryNumber],[StockNo],[Receiver],[Deliverer],[R_Date],[MonthCurr])" _
                & " VALUES ('XSX','" & ReceiptNumber & "','" & StockNo & "','0004','0001','" & Format(Now(), "yyyy-MMM-dd") & "','" & Format(Now(), "MM/yyyy") & "')"
                If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
                    MessageBox.Show("chua Cap nhat Delivery. tam ngung di")
                    Exit Function
                End If
                For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                    MySQLStr = "Select * from Delivery_sp where Workorder ='" & ds.Tables(0).Rows(i)("RMMNote") & "'  and Deliverynumber ='" & ReceiptNumber & "' and PartNo ='" & Trim(ds.Tables(0).Rows(i)("PartName")) & "'"
                    ds1 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                    If ds1.Tables.Count <> 0 AndAlso ds1.Tables(0).Rows.Count = 1 Then
                        MySQLStr = "UPDATE Delivery_sp SET [UnitWO] = [UnitWO]  + " & ds.Tables(0).Rows(i)("Qty") & ", [UnitDelivery]= [UnitDelivery]+ " & ds.Tables(0).Rows(i)("Qty") & ", [UnitBarcode]= [UnitBarcode]+ " & ds.Tables(0).Rows(i)("Qty") & "  where Workorder ='" & ds.Tables(0).Rows(i)("RMMNote") & "'  and Deliverynumber ='" & ReceiptNumber & "' and PartNo ='" & Trim(ds.Tables(0).Rows(i)("PartName")) & "'"
                        If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
                            MessageBox.Show("chua Cap nhat da Delivery_sp . tam ngung di")
                            Exit Function
                        End If
                    Else
                        MySQLStr = "INSERT INTO [Delivery_sp]([DeliveryNumber],[worKorder],[PartNo],[UnitWO],[UnitDelivery],[UnitBarcode])" _
                        & " VALUES ('" & ReceiptNumber & "','" & ds.Tables(0).Rows(i)("RMMNote") & "','" & Trim(ds.Tables(0).Rows(i)("PartName")) & "'," & ds.Tables(0).Rows(i)("Qty") & "," & ds.Tables(0).Rows(i)("Qty") & "," & ds.Tables(0).Rows(i)("Qty") & ")"
                        If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
                            MessageBox.Show("chua Cap nhat Receipt_sp. tam ngung di")
                            Exit Function
                        End If
                    End If
                    MySQLStr = "UPDATE WH_MaterialTransfer SET [DeliveryNote] ='" & ReceiptNumber & "' where ID ='" & ds.Tables(0).Rows(i)("ID") & "'"
                    If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = False Then
                        MessageBox.Show("chua Cap nhat da in nhan. tam ngung di")
                        Exit Function
                    End If
                Next


            End If
            MySQLStr = "SELECT PartName, RMMNote, sum(Qty) as Qty, Code, Marking, StockNo,DeliveryNote  FROM WH_MaterialTransfer GROUP BY PartName, RMMNote,  Code, Marking, StockNo,DeliveryNote HAVING (Marking = '1' or  Marking = '2' or  Marking = '3'  or  Marking = '4' ) and RMMNote = '" & RMMNote & "'ORDER BY PartName"
            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                dsObj.MainTable.Clear()
                For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                    MyResult = True
                    GetWHDES(Trim(ds.Tables(0).Rows(i)("PartName")), MyPDes, MyPUnit)
                    dsObj.MainTable.AddMainTableRow(i + 1, MyPDes, ds.Tables(0).Rows(i)("StockNo"), ds.Tables(0).Rows(i)("PartName"), MyPUnit, ds.Tables(0).Rows(i)("RMMNote"), ds.Tables(0).Rows(i)("Qty"), ds.Tables(0).Rows(i)("Qty"), 0, CurrentUserName, "", "", "", ds.Tables(0).Rows(i)("DeliveryNote"))
                Next
                MySQLStr = "UPDATE WH_MaterialTransfer SET Marking ='2'where [DeliveryNote] ='" & ReceiptNumber & "'"
                If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = False Then
                    MessageBox.Show("chua Cap nhat da in nhan. tam ngung di")
                    Exit Function
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString())
            ErrRecording("", "frmMain/UpdateDeliveryNoteWH2", ex.Message)
        End Try
        Return MyResult
    End Function

    Private Function RRMNote(ByVal RMMNote As String) As Boolean
        Dim MyResult As Boolean = False
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Dim ds1 As DataSet
        Dim MyPDes As String = ""
        Dim MyPUnit As String = ""
        dsObj.MainTable.Clear()
        Try
            MySQLStr = "SELECT VenderCompany, PartNo, PO, Sum(Qty) AS Qty, Code, Marking, ReceiptNumber FROM WH_ReceiptMaterial GROUP BY PartNo, PO, Code, Marking, ReceiptNumber,VenderCompany HAVING Marking = '4' and ReceiptNumber = '" & RMMNote & "' "
            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                dsObj.MainTable.Clear()
                MySQLStr = "Select * from WH_ReceiptMaterial where  Marking = '4' and ReceiptNumber ='" & RMMNote & "' ORDER BY ARMTime"
                ds1 = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
                If ds1.Tables.Count <> 0 AndAlso ds1.Tables(0).Rows.Count > 0 Then
                    For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                        MyResult = True
                        GetWHDES(Trim(ds.Tables(0).Rows(i)("PartNo")), MyPDes, MyPUnit)
                        'ElseIf GetWHDES_local(Trim(ds.Tables(0).Rows(i)("PartNo")), ds1.Tables(0).Rows(0)("AReceiptNumber"), MyPDes, MyPUnit) = True Then
                        'Else
                        '    MessageBox.Show("Loi roi")
                        '    Exit Function
                        'End If
                        dsObj.MainTable.AddMainTableRow(i + 1, ds.Tables(0).Rows(i)("PartNo"), StockNo, ds.Tables(0).Rows(i)("PO"), MyPUnit, ds.Tables(0).Rows(i)("VenderCompany"), ds.Tables(0).Rows(i)("Qty"), ds.Tables(0).Rows(i)("Qty"), "", CurrentUserName, "Thermtrol/Warehouse", Format(ds1.Tables(0).Rows(0)("ARMTime"), "dd-MMM-yy"), "", ds.Tables(0).Rows(i)("ReceiptNumber"))
                    Next
                End If
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/RRMNote", ex.Message)
            MyResult = False
        Finally
        End Try
        Return MyResult
    End Function

    Private Function CheckQtyRMMandPicking(ByVal RMMNote As String, ByRef QtyRMM As String, ByRef QtyPicking As String) As Boolean
        Dim MyResult As Boolean = False
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Try
            MySQLStr = "SELECT * FROM WH_MaterialTransfer WHERE RMMNote = '" & RMMNote & "'"
            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                QtyRMM = ds.Tables(0).Rows.Count
            End If
            MySQLStr = "SELECT * FROM WH_MovingMaterial WHERE RMMNote = '" & RMMNote & "'"
            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                QtyPicking = ds.Tables(0).Rows.Count
            End If
            If CInt(QtyRMM) = CInt(QtyPicking) Then
                MyResult = True
            Else
                MyResult = False
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/CheckQtyRMMandPicking", ex.Message)
            MyResult = False
        Finally
        End Try
        Return MyResult
    End Function

    Private Function LoadDeliveryNote(ByVal MyDeliveryNote As String) As Boolean
        Dim Myresult As Boolean = False
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Dim DQty As Integer = 0
        Try
            MySQLStr = "SELECT * FROM DeliveryWPD where Deliverynumber ='" & MyDeliveryNote & "'"
            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                dsObj.MainTable.Clear()
                For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                    'dsObj.MainTable.AddMainTableRow(ds.Tables(0).Rows(i)("IDRecord"), IIf(ds.Tables(0).Rows(i)("partno") Is DBNull.Value, "N", ds.Tables(0).Rows(i)("partno")), ds.Tables(0).Rows(i)("StockNo"), CInt(ds.Tables(0).Rows(i)("inventory")), IIf(ds.Tables(0).Rows(i)("l2") Is DBNull.Value, " ", ds.Tables(0).Rows(i)("l2")), IIf(SLoc Is DBNull.Value, " ", SLoc), MyRD)
                    '([ID],[WO],[Deliverynumber],[PartNo],[UnitWO],[UnitDelivery],[DeliveryTime])" _DeliveryTime

                    dsObj.MainTable.AddMainTableRow(i + 1, ds.Tables(0).Rows(i)("wo"), ds.Tables(0).Rows(i)("PartNo"), ds.Tables(0).Rows(i)("UnitWO"), ds.Tables(0).Rows(i)("UnitDelivery"), ds.Tables(0).Rows(i)("DeliveryTime"), ds.Tables(0).Rows(i)("Deliverynumber"), "", "", "", "", "", "", "")
                    MySQLStr = "UPDATE DeliveryWPD SET PrintTimes = PrintTimes + 1 WHERE ID = '" & ds.Tables(0).Rows(i)("ID") & "'"
                    If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = False Then
                        ErrRecording("", "frmMain/LoadDeliveryNote", "KO cap nhat dc DATA" & MySQLStr)
                    End If
                Next
                frmDeliveryNote.Show()
                Myresult = True
            Else
                ErrRecording("", "frmMain/LoadDeliveryNote", "KO LOAD DC DATA" & MySQLStr)
                MessageBox.Show("KHONG LOAD DC PHIEU XUAT KHO AO ")
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/LoadDeliveryNote", ex.Message)
            Myresult = False
        End Try
        Return Myresult
    End Function

    Private Function LoadPartAtPC() As Boolean
        Dim MyResult As Boolean = False
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Dim MyWIP As String = ""
        Me.ViewData.Rows.Clear()
        Dim MyRD As String = ""
        Me.ViewData.BackgroundColor = Color.Beige
        Dim MyStockNo As String = ""
        Me.ViewData.BackgroundColor = Color.Bisque
        Me.ViewData.Columns("No").HeaderText = "No"
        Me.ViewData.Columns("No").Width = 30
        Me.ViewData.Columns("Customer").HeaderText = "PartName"
        Me.ViewData.Columns("Customer").Width = 100
        Me.ViewData.Columns("PartN").HeaderText = "Qty"
        Me.ViewData.Columns("PartN").Width = 100
        Me.ViewData.Columns("Wordorder").HeaderText = "" '"Qty"
        Me.ViewData.Columns("Wordorder").Width = 50
        Me.ViewData.Columns("OutQty").HeaderText = "" ' "Qty"
        Me.ViewData.Columns("OutQty").Width = 60
        Me.ViewData.Columns("Delivery").HeaderText = "" '"Delivery"
        Me.ViewData.Columns("Delivery").Width = 60
        Me.ViewData.Columns("WIP").HeaderText = "" '"WHWIP"
        Me.ViewData.Columns("WIP").Width = 60
        Me.ViewData.Columns("BAL").HeaderText = "" '"WH BAL"
        Me.ViewData.Columns("BAL").Width = 60
        Me.ViewData.Columns("PDQty").HeaderText = "" '"PDQty"
        Me.ViewData.Columns("PDQty").Width = 60
        Me.ViewData.Columns("PDRemain").HeaderText = "" '"PDRemain"
        Me.ViewData.Columns("PDRemain").Width = 60
        Me.ViewData.Columns("Comment").HeaderText = "" '"Comment"
        Me.ViewData.Columns("Comment").Width = 120
        Me.ViewData.Columns("PartArriveTime").HeaderText = "" '"PartArriveTime"
        Me.ViewData.Columns("PartArriveTime").Width = 100
        Me.ViewData.Columns("PDQty").Visible = False
        Me.ViewData.Columns("PDRemain").Visible = False
        Try
            If MyPartLOC = "*" Then
                'MySQLStr = "SELECT * from PDRemainQty order by UpdateTime desc"
                MySQLStr = " SELECT PartNo, Sum(ActWIP) AS ActWIP FROM PDRemainQty GROUP BY PartNo  HAVING Sum(ActWIP)>0"

            Else
                'MySQLStr = "SELECT * from PDRemainQty where PartNo ='" & MyPartLOC & "'"

                MySQLStr = " SELECT PartNo,  Sum(ActWIP) AS ActWIP FROM PDRemainQty GROUP BY PartNo HAVING Sum(ActWIP)>0 and PartNo ='" & MyPartLOC & "'"
            End If
            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                dsObj.MainTable.Clear()
                For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                    Me.ViewData.Rows.Add()
                    Me.ViewData.Rows(i).Cells("No").Value = i + 1
                    Me.ViewData.Rows(i).Cells("Customer").Value = ds.Tables(0).Rows(i)("PartNo")
                    'Me.ViewData.Rows(i).Cells("PartN").Value = ds.Tables(0).Rows(i)("WO")
                    Me.ViewData.Rows(i).Cells("PartN").Value = ds.Tables(0).Rows(i)("ActWIP")
                    MyResult = True
                    '                    dsObj.MainTable.AddMainTableRow(ds.Tables(0).Rows(i)("ID"), IIf(ds.Tables(0).Rows(i)("partid") Is DBNull.Value, "N", ds.Tables(0).Rows(i)("partid")), IIf(ds.Tables(0).Rows(i)("WHQTY") Is DBNull.Value, "NONE", (ds.Tables(0).Rows(i)("WHQTY"))) & " / " & IIf(ds.Tables(0).Rows(i)("STQTY") Is DBNull.Value, "NONE", CInt(ds.Tables(0).Rows(i)("STQTY"))))
                    dsObj.MainTable.AddMainTableRow(i + 1, ds.Tables(0).Rows(i)("PartNo"), "", ds.Tables(0).Rows(i)("ActWIP"), "", "", "", "", "", "", "", "", "", "")

                Next
            Else
                MyResult = False
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/LoadPartAtPC", ex.Message)
            MyResult = False
        End Try
        Return MyResult
    End Function


    Private Function LoadStockTakingPartLocation() As Boolean
        Dim MyResult As Boolean = False
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Dim MyWIP As String = ""
        Me.ViewData.Rows.Clear()
        Dim MyRD As String = ""
        Me.ViewData.BackgroundColor = Color.Beige
        Dim MyStockNo As String = ""
        Try
            'MySQLStr = "DELETE FROM [PrintTemp]"
            'If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = False Then
            '    MessageBox.Show("CHUA XOA DUOC BANG IN TAO. KO NEN IN NHA")
            'End If
            If MyPartLOC = "*" Then
                MySQLStr = "SELECT * from PartStockTaking order by PartID"
            ElseIf MyPartLOC.Length = 2 AndAlso Mid(MyPartLOC, 2, 1) = "*" Then
                MySQLStr = "SELECT * from PartStockTaking where substring(Shelf,1,1) ='" & Mid(MyPartLOC, 1, 1) & "' order by PartID "
            Else
                MySQLStr = "SELECT * from PartStockTaking where Shelf ='" & MyPartLOC & "' order by partID"
            End If
            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                dsObj.MainTable.Clear()
                Me.ViewData.Columns("Customer").HeaderText = "ID"
                Me.ViewData.Columns("Wordorder").HeaderText = "StockNo"
                Me.ViewData.Columns("PartArriveTime").HeaderText = ""
                Me.ViewData.Columns("Delivery").HeaderText = "Location"
                Me.ViewData.Columns("BAL").HeaderText = "Reading Time"
                Me.ViewData.Columns("WIP").HeaderText = "WH Loc"
                Me.ViewData.Columns("OutQty").HeaderText = "WHQty/STQty"
                Me.ViewData.Columns("Comment").HeaderText = "S-W"
                Me.ViewData.Columns("PDQty").Visible = False
                Me.ViewData.Columns("PDRemain").Visible = False
                For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                    Me.ViewData.Rows.Add()
                    Dim WQ As Integer = IIf(ds.Tables(0).Rows(i)("WHQTY") Is DBNull.Value, 0, (ds.Tables(0).Rows(i)("WHQTY")))
                    Dim SQ As Integer = IIf(ds.Tables(0).Rows(i)("STQTY") Is DBNull.Value, 0, CInt(ds.Tables(0).Rows(i)("STQTY")))
                    Dim BQ As Integer = SQ - WQ
                    Dim WL As String = GetWHLOC(ds.Tables(0).Rows(i)("ID"), MyStockNo)
                    Me.ViewData.Rows(i).Cells("No").Value = i + 1
                    Me.ViewData.Rows(i).Cells("Customer").Value = ds.Tables(0).Rows(i)("ID")
                    Me.ViewData.Rows(i).Cells("PartN").Value = ds.Tables(0).Rows(i)("partid")
                    Me.ViewData.Rows(i).Cells("Wordorder").Value = MyStockNo
                    Me.ViewData.Rows(i).Cells("OutQty").Value = IIf(ds.Tables(0).Rows(i)("WHQTY") Is DBNull.Value, "NONE", (ds.Tables(0).Rows(i)("WHQTY"))) & " / " & IIf(ds.Tables(0).Rows(i)("STQTY") Is DBNull.Value, "NONE", CInt(ds.Tables(0).Rows(i)("STQTY")))
                    Me.ViewData.Rows(i).Cells("WIP").Value = WL
                    Me.ViewData.Rows(i).Cells("Delivery").Value = ds.Tables(0).Rows(i)("Shelf")
                    Me.ViewData.Rows(i).Cells("Comment").Value = BQ
                    Me.ViewData.Rows(i).Cells("BAL").Value = Format(ds.Tables(0).Rows(i)("ReadingTime"), "dd-MMM-yy hh:mm:ss")

                    If WL.Length < 4 Then
                        Dim l1 As String = "000" & Mid(WL, 2)
                        l1 = Mid(WL, 1, 1) & Mid(l1, Len(l1) - 2)
                        If Trim(l1) <> Trim(ds.Tables(0).Rows(i)("Shelf")) Then
                            Me.ViewData.Rows(i).Cells("Delivery").Style.BackColor = Color.Cornsilk
                        End If
                    Else
                        If Trim(WL) <> Trim(ds.Tables(0).Rows(i)("Shelf")) Then
                            Me.ViewData.Rows(i).Cells("Delivery").Style.BackColor = Color.Cornsilk
                        End If
                    End If


                    If BQ > 0 Then
                        Me.ViewData.Rows(i).Cells("Comment").Style.BackColor = Color.Aqua
                    ElseIf BQ < 0 Then
                        Me.ViewData.Rows(i).Cells("Comment").Style.BackColor = Color.Brown
                    End If

                    Me.ViewData.Rows(i).Cells("PartArriveTime").Value = ""
                    MyResult = True

                    '                    dsObj.MainTable.AddMainTableRow(ds.Tables(0).Rows(i)("ID"), IIf(ds.Tables(0).Rows(i)("partid") Is DBNull.Value, "N", ds.Tables(0).Rows(i)("partid")), IIf(ds.Tables(0).Rows(i)("WHQTY") Is DBNull.Value, "NONE", (ds.Tables(0).Rows(i)("WHQTY"))) & " / " & IIf(ds.Tables(0).Rows(i)("STQTY") Is DBNull.Value, "NONE", CInt(ds.Tables(0).Rows(i)("STQTY"))))
                    dsObj.MainTable.AddMainTableRow(ds.Tables(0).Rows(i)("ID"), IIf(ds.Tables(0).Rows(i)("partid") Is DBNull.Value, "N", ds.Tables(0).Rows(i)("partid")), MyStockNo, IIf(ds.Tables(0).Rows(i)("WHQTY") Is DBNull.Value, "NONE", (ds.Tables(0).Rows(i)("WHQTY"))) & " / " & IIf(ds.Tables(0).Rows(i)("STQTY") Is DBNull.Value, "NONE", CInt(ds.Tables(0).Rows(i)("STQTY"))), WL, ds.Tables(0).Rows(i)("Shelf"), Format(ds.Tables(0).Rows(i)("ReadingTime"), "dd-MMM-yy hh:mm:ss"), "", "", "", "", "", "", "")

                Next
            Else
                MyResult = False
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/LoadStockTakingPartLocation", ex.Message)
            MyResult = False
        End Try
        Return MyResult
    End Function
    Private Function LoadPartLocation() As Boolean
        Dim MyResult As Boolean = False
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Dim MyWIP As String = ""
        Me.ViewData.Rows.Clear()
        Dim MyRD As String = ""
        Me.ViewData.BackgroundColor = Color.Beige
        Try
            'MySQLStr = "DELETE FROM [PrintTemp]"
            'If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = False Then
            '    MessageBox.Show("CHUA XOA DUOC BANG IN TAO. KO NEN IN NHA")
            'End If
            'MySQLStr = "SELECT * from tbInventory where location ='" & MyPartLOC & "' and inventory > 0 order by trace"
            If MyPartLOC.Length = 1 AndAlso Mid(MyPartLOC, 1, 1) = "*" Then
                If Me.rbPart.Checked = True Then
                    MySQLStr = " SELECT IDRecord, StockNo, PartNo,  Receipt, Delivery, Inventory, Trace, tbInventory.Location as L1, IDandLocation.Location as L2 FROM IDandLocation RIGHT JOIN tbInventory ON IDandLocation.ID = tbInventory.IDRecord WHERE Inventory > 0  ORDER BY IDandLocation.Location"
                End If
            ElseIf MyPartLOC.Length = 2 AndAlso Mid(MyPartLOC, 2, 1) = "*" Then
                If Me.rbLOC.Checked = True Then
                    MyDisplayFunctionFlg = "3"
                    MySQLStr = " SELECT IDRecord, StockNo, PartNo,  Receipt, Delivery, Inventory, Trace, tbInventory.Location as L1, IDandLocation.Location as L2 FROM IDandLocation RIGHT JOIN tbInventory ON IDandLocation.ID = tbInventory.IDRecord WHERE Inventory > 0 And substring(IDandLocation.Location,1,1) = '" & Mid(MyPartLOC, 1, 1) & "' ORDER BY Trace"
                End If
            Else
                If Me.rbLOC.Checked = True Then
                    MySQLStr = " SELECT IDRecord, StockNo, PartNo,  Receipt, Delivery, Inventory, Trace, tbInventory.Location as L1, IDandLocation.Location as L2 FROM IDandLocation RIGHT JOIN tbInventory ON IDandLocation.ID = tbInventory.IDRecord WHERE Inventory > 0 And IDandLocation.Location = '" & MyPartLOC & "' ORDER BY IDandLocation.Location"
                ElseIf Me.rbPart.Checked = True Then
                    MyDisplayFunctionFlg = "4"
                    MySQLStr = " SELECT IDRecord, StockNo, PartNo,  Receipt, Delivery, Inventory, Trace, tbInventory.Location as L1, IDandLocation.Location as L2 FROM IDandLocation RIGHT JOIN tbInventory ON IDandLocation.ID = tbInventory.IDRecord WHERE Inventory > 0 And PartNo = '" & MyPartLOC & "' ORDER BY IDRecord, StockNo"
                ElseIf Me.rdIDR.Checked = True Then
                    MyDisplayFunctionFlg = "9"
                    MySQLStr = " SELECT IDRecord, StockNo, PartNo,  Receipt, Delivery, Inventory, Trace, tbInventory.Location as L1, IDandLocation.Location as L2 FROM IDandLocation RIGHT JOIN tbInventory ON IDandLocation.ID = tbInventory.IDRecord WHERE tbInventory.IDRecord = " & MyPartLOC & " ORDER BY IDandLocation.Location"
                End If

            End If
            ds = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                dsObj.MainTable.Clear()
                Me.ViewData.Columns("Customer").HeaderText = "ID"
                Me.ViewData.Columns("Wordorder").HeaderText = "StockNo"
                Me.ViewData.Columns("PartArriveTime").HeaderText = ""
                Me.ViewData.Columns("Delivery").HeaderText = "Location"
                Me.ViewData.Columns("BAL").HeaderText = "Receip Date dd-MMM-yy"
                Me.ViewData.Columns("WIP").HeaderText = "S Loc"
                Me.ViewData.Columns("Comment").HeaderText = "W Loc"
                Me.ViewData.Columns("OutQty").HeaderText = "Qty"
                Me.ViewData.Columns("PDQty").Visible = False
                Me.ViewData.Columns("PDRemain").Visible = False
                ' Me.ViewData.Columns("OutQty").HeaderText = "Qty"

                For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                    Me.ViewData.Rows.Add()
                    Dim SLoc As String = GetSLOC(ds.Tables(0).Rows(i)("IDRecord"))
                    Dim SLoc1 As String = GetSLOC1(ds.Tables(0).Rows(i)("IDRecord"))
                    Me.ViewData.Rows(i).Cells("No").Value = i + 1
                    Me.ViewData.Rows(i).Cells("Customer").Value = ds.Tables(0).Rows(i)("IDRecord")
                    Me.ViewData.Rows(i).Cells("PartN").Value = ds.Tables(0).Rows(i)("partno")
                    Me.ViewData.Rows(i).Cells("Wordorder").Value = ds.Tables(0).Rows(i)("StockNo")
                    Me.ViewData.Rows(i).Cells("OutQty").Value = CInt(ds.Tables(0).Rows(i)("inventory"))
                    Me.ViewData.Rows(i).Cells("WIP").Value = SLoc
                    Me.ViewData.Rows(i).Cells("Delivery").Value = ds.Tables(0).Rows(i)("l2")
                    Me.ViewData.Rows(i).Cells("Comment").Value = SLoc1 'ds.Tables(0).Rows(i)("l1")
                    MyRD = ds.Tables(0).Rows(i)("trace")
                    MyRD = Mid(MyRD, 5, 2) & "-" & Mid(MyRD, 3, 2) & "-" & Mid(MyRD, 1, 2)
                    Me.ViewData.Rows(i).Cells("BAL").Value = MyRD

                    'Dim strDate As String
                    'Dim objdate As Date
                    'strDate = ds.Tables(0).Rows(i)("trace")
                    'strDate = Mid(strDate, 5, 2) & "-" & Mid(strDate, 3, 2) & "-" & Mid(strDate, 1, 2) 'strDate.Substring(0, 2) & "/" & strDate.Substring(2, 2) & "/" & strDate.Substring(4, 4)
                    'objdate = Date.Parse(strDate).ToShortDateString
                    'objdate = Format(objdate, "dd-MMM-yy")
                    'Me.ViewData.Rows(i).Cells("BAL").Value = objdate


                    'Dim strDate As String
                    'Dim objdate As Date
                    'strDate = "11301982"
                    'strDate = strDate.Substring(0, 2) & "/" & strDate.Substring(2, 2) & "/" & strDate.Substring(4, 4)
                    'objdate = Date.Parse(strDate).ToShortDateString
                    Dim WL As String = ""
                    If ds.Tables(0).Rows(i)("l2") Is DBNull.Value = True Then
                        WL = ""
                    Else
                        WL = Trim(ds.Tables(0).Rows(i)("l2"))
                    End If

                    If WL.Length < 4 Then
                        Dim l1 As String = "000" & Mid(WL, 2)
                        l1 = Mid(WL, 1, 1) & Mid(l1, Len(l1) - 2)
                        If Trim(l1) <> Trim(SLoc) Then
                            Me.ViewData.Rows(i).Cells("Delivery").Style.BackColor = Color.Cornsilk
                        End If
                    Else
                        If Trim(WL) <> Trim(SLoc) Then
                            Me.ViewData.Rows(i).Cells("Delivery").Style.BackColor = Color.Cornsilk
                        End If
                    End If



                    MyResult = True
                    dsObj.MainTable.AddMainTableRow(ds.Tables(0).Rows(i)("IDRecord"), IIf(ds.Tables(0).Rows(i)("partno") Is DBNull.Value, "N", ds.Tables(0).Rows(i)("partno")), ds.Tables(0).Rows(i)("StockNo"), CLng(ds.Tables(0).Rows(i)("inventory")), IIf(ds.Tables(0).Rows(i)("l2") Is DBNull.Value, " ", ds.Tables(0).Rows(i)("l2")), IIf(SLoc Is DBNull.Value, " ", SLoc), MyRD, "", "", "", "", "", "", "")
                Next
            Else
                MyResult = False
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/LoadShortageMaterial", ex.Message)
            MyResult = False
        End Try
        Return MyResult
    End Function

    Private Function CreatePalletNo(ByVal No As String) As Boolean
        Dim MyResult As Boolean = False
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        No = "000" & No
        No = Mid(No, No.Length - 2, 3)
        'Dim IDDD As Array

        ''Create an instance of Linear Barcode
        ''Use BarcodeLib.Barcode.DataMatrix.DataMatrix for Data Matrix
        ''Use BarcodeLib.Barcode.PDF417.PDF417 for PDF417
        ''Use BarcodeLib.Barcode.PDF417.MicroPDF417 for MicroPDF417
        'BarcodeLib.Barcode.Linear.Linear(IDDD = New BarcodeLib.Barcode.Linear.Linear())
        'IDDD.Type = BarcodeLib.Barcode.Linear.BarcodeType.CODE39
        'IDDD.BarHeight = 50 '50 pixel
        'IDDD.Format = ImageFormat.Png

        '        Dim BBB As new BaseCollection   = BarcodeLib.Barcode.Linear.BarcodeType.CODE39


        Try
            If IsNumeric(No) = True And CInt(No) < 1000 Then
                dsObj.MainTable.Clear()
                MySQLStr = "SELECT * FROM WH_Shelf WHERE Shelf = '" & "Y" & No & "'"
                ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
                If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count = 0 Then
                    MySQLStr = "INSERT INTO [WH_Shelf]([Shelf],[StockNo],[Process])" _
                    & " VALUES ('" & "Y" & No & " ','" & StockNo & "','Pallet')"
                    If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = False Then
                        MessageBox.Show("Dữ liệu bị lỗi INSERT WH_Shelf " & MySQLStr)
                        MyResult = False
                    Else
                        MyResult = True

                        dsObj.MainTable.AddMainTableRow("*Y" & No & "*", CInt(No), "Pallet", "", "", "", "", "", "", "", "", "", "", "")
                    End If
                Else
                    MessageBox.Show("Pallet: " & CInt(No) & ". Đã in rồi. Bạn muốn in lại à ")
                    MyResult = True
                    dsObj.MainTable.AddMainTableRow("*Y" & No & "*", CInt(No), "Pallet", "", "", "", "", "", "", "", "", "", "", "")
                End If
            Else
                MessageBox.Show("Yêu cầu nhập số và nhỏ hơn 1000")
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/CreatePalletNo", ex.Message)
            MyResult = ""
        Finally
        End Try
        Return MyResult
    End Function

    Private Function GetWHLOC(ByVal MyID As String, ByRef StockNo As String) As String
        Dim MyResult As String = ""
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Try
            If IsNumeric(MyID) = True Then
                MySQLStr = "SELECT * FROM IDandLocation WHERE ID = '" & Trim(MyID) & "'"
                ds = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count = 1 Then
                    MyResult = ds.Tables(0).Rows(0)("location")
                    StockNo = "WH_01" ' ds.Tables(0).Rows(0)("StockNo")
                End If
            Else
                MessageBox.Show("Yêu cầu nhập số")
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/GetWHLOC", ex.Message)
            MyResult = ""
        Finally
        End Try
        Return MyResult
    End Function

    Private Function GetSLOC(ByVal MyID As String) As String
        Dim MyResult As String = ""
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Try
            If IsNumeric(MyID) = True Then
                MySQLStr = "SELECT * FROM PartStockTaking WHERE ID = '" & Trim(MyID) & "'"
                ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
                If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count = 1 Then
                    MyResult = ds.Tables(0).Rows(0)("Shelf")
                End If
            Else
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/GetSLOC", ex.Message)
            MyResult = ""
        Finally
        End Try
        Return MyResult
    End Function

    Private Function GetSLOC1(ByVal MyID As String) As String
        Dim MyResult As String = ""
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Try
            If IsNumeric(MyID) = True Then
                MySQLStr = "SELECT * FROM WH_PartStockTaking WHERE ID = '" & Trim(MyID) & "'"
                ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
                If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count = 1 Then
                    MyResult = ds.Tables(0).Rows(0)("Shelf")
                End If
            Else
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/GetSLOC1", ex.Message)
            MyResult = ""
        Finally
        End Try
        Return MyResult
    End Function

    Private Sub PrintToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem.Click
        '  frmPrintData.Show()
        Me.lblTitle.BackColor = Color.Yellow
        frmReportI.Show()
    End Sub

    Private Sub UpdatePartNameToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdatePartNameToolStripMenuItem.Click
        Me.lblTitle.BackColor = Color.Yellow
        UpdatePartName()
    End Sub
    Private Sub UpdatePartName()
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Dim ds1 As DataSet
        Try
            MySQLStr = "SELECT * from PartStockTaking where Marking = '1' "
            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                    MySQLStr = "SELECT * from tbinventory where IDRecord ='" & ds.Tables(0).Rows(i)("ID") & "'"
                    ds1 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                    If ds1.Tables.Count <> 0 AndAlso ds1.Tables(0).Rows.Count = 1 Then
                        MySQLStr = "UPDATE PartStockTaking SET Marking = '2',WHQty= '" & ds1.Tables(0).Rows(0)("inventory") & "', PartID ='" & ds1.Tables(0).Rows(0)("PartNo") & "' " _
                            & "WHERE ID = '" & ds1.Tables(0).Rows(0)("IDRecord") & "'"
                        If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = False Then
                            MessageBox.Show("Cap nhat PartName ko dc :" & ds1.Tables(0).Rows(0)("PartNo"))
                        End If
                    Else
                        MessageBox.Show("Ko tim thay ID :" & ds.Tables(0).Rows(0)("ID"))
                    End If
                Next
                MessageBox.Show("Da cap nhat xong " & ds.Tables(0).Rows.Count & " record")
            Else
                MessageBox.Show("Da cap roi")
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/UpdatePartName", ex.Message)
        End Try
    End Sub

    '    Private Function BalanceMoWO() As Boolean
    '        Dim MyResult As Boolean = False
    '        Dim MySQLStr As String = ""
    '        Dim ds As DataSet
    '        Me.ViewData.Rows.Clear()
    '        Dim MyWIP As String = ""
    '        Dim MyOUTPUT As String = ""
    '        Dim MyIDCom As String = ""
    '        Dim MyCommnet As String = ""
    '        Dim MyPATime As String = ""
    '        Me.ViewData.BackgroundColor = Color.Bisque
    '        Me.ViewData.Columns("BAL").HeaderText = "WH BAL"
    '        Me.ViewData.Columns("Customer").HeaderText = "Customer"
    '        Me.ViewData.Columns("Delivery").HeaderText = "Delivery"
    '        Me.ViewData.Columns("WIP").HeaderText = "WHWIP"
    '        Me.ViewData.Columns("Comment").HeaderText = "Comment"
    '        Me.ViewData.Columns("OutQty").HeaderText = "Qty"
    '        Me.ViewData.Columns("PartArriveTime").HeaderText = "Ship date"
    '        Me.ViewData.Columns("PDQty").Visible = False
    '        Me.ViewData.Columns("PDRemain").Visible = False
    '        Dim St As Date = Me.St.Text
    '        Dim Et As Date = Me.Et.Text

    '        Try
    '            If MyDBConnection.DBConnecting = True Then
    '                MySQLStr = "SELECT wono, custno, item, Sum(oqty) AS SQTY ,shipdte FROM matran10 GROUP BY wono, custno, item, shipdte   HAVING  ( shipdte > #" & St & "# And shipdte < #" & Et & "# ) order by shipdte"
    '                ds = MyDBConnection.ExecuteProcedureDBSVR(MySQLStr)
    '                If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
    '                    For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
    '                        MyWO = Trim(ds.Tables(0).Rows(i)("wono"))
    '                        MyCommnet = ""
    '                        MyPATime = ""
    '                        MyIDCom = ds.Tables(0).Rows(i)("wono") & "-" & ds.Tables(0).Rows(i)("item")
    '                        MyWIP = GETWIP(ds.Tables(0).Rows(i)("item"))
    '                        MyOUTPUT = GETOUTPUT(ds.Tables(0).Rows(i)("item"))
    '                        GETCommentData(MyIDCom, MyCommnet, MyPATime)
    '                        Me.ViewData.Rows.Add()
    '                        If Mid(ds.Tables(0).Rows(i)("item"), 1, 2) = "CD" Then
    '                            MyShipDate = ds.Tables(0).Rows(i)("shipdte")
    '                            GoTo Skip
    '                        ElseIf Mid(ds.Tables(0).Rows(i)("item"), 1, 1) = "L" Then
    '                            GoTo Skip
    '                        End If
    '                        If CInt(MyOUTPUT) >= CInt(ds.Tables(0).Rows(i)("SQTY")) Then
    '                            If MyBMoWO = True Then GoTo Skip
    '                        End If
    '                        Me.ViewData.Rows(i).Cells("No").Value = i + 1
    '                        Me.ViewData.Rows(i).Cells("Customer").Value = ds.Tables(0).Rows(i)("custno")
    '                        Me.ViewData.Rows(i).Cells("PartN").Value = ds.Tables(0).Rows(i)("item")
    '                        Me.ViewData.Rows(i).Cells("Wordorder").Value = ds.Tables(0).Rows(i)("wono")
    '                        Me.ViewData.Rows(i).Cells("OutQty").Value = CInt(ds.Tables(0).Rows(i)("SQTY"))
    '                        Me.ViewData.Rows(i).Cells("WIP").Value = MyWIP
    '                        Me.ViewData.Rows(i).Cells("BAL").Value = CInt(MyWIP) - CInt(ds.Tables(0).Rows(i)("SQTY"))
    '                        If CInt(MyWIP) - CInt(ds.Tables(0).Rows(i)("SQTY")) < 0 Then
    '                            Me.ViewData.Rows(i).Cells("BAL").Style.BackColor = Color.Blue
    '                        End If
    '                        Me.ViewData.Rows(i).Cells("Delivery").Value = MyOUTPUT

    '                        If CInt(MyOUTPUT) < CInt(ds.Tables(0).Rows(i)("SQTY")) Then
    '                            Me.ViewData.Rows(i).Cells("Delivery").Style.BackColor = Color.Aquamarine
    '                        End If
    '                        Me.ViewData.Rows(i).Cells("Comment").Value = MyCommnet
    '                        Me.ViewData.Rows(i).Cells("PartArriveTime").Value = MyShipDate
    '                        'If MyPATime <> "" Then
    '                        '    Dim t2 As Date = MyPATime
    '                        '    Me.ViewData.Rows(i).Cells("PartArriveTime").Value = MyShipDate Format(t2, "dd-MMM-yy hh:mm:ss")
    '                        'End If
    'Skip:
    '                    Next
    '                Else
    '                    MyResult = False
    '                    MessageBox.Show("WO NÀY TÌM ĐƯỢC DỮ LIỆU TRONG ACCPAC BẢNG MATRAN10" & MySQLStr)
    '                    ErrRecording("", "frmMain/BalanceMoWO", "WO NÀY TÌM ĐƯỢC DỮ LIỆU TRONG MATRAN10 " & MySQLStr)
    '                End If
    '            Else
    '                MyResult = False
    '                MessageBox.Show("KHÔNG KẾT NỐI SERVER ACCPAC CỤC BỘ")
    '            End If
    '        Catch ex As Exception
    '            ErrRecording("", "frmMain/BalanceMoWO", ex.Message)
    '            MessageBox.Show("LOI. BAO CAP TREN")
    '        Finally
    '            If MyDBConnection IsNot Nothing Then
    '                MyDBConnection.DBClosing()
    '            End If
    '        End Try
    '        Return MyResult
    '    End Function

    'Private Function BalUnitWoDelivery() As Boolean
    '    Dim MyResult As Boolean = False
    '    Dim MySQLStr As String = ""
    '    Dim ds As DataSet
    '    Dim ds1 As DataSet
    '    Dim MyID As String = ""
    '    Dim MyOUTPUT As String = ""
    '    Dim MyWQty As String = ""
    '    Dim ReQty As Integer = 0
    '    Dim St As Date = (DateAdd(DateInterval.Month, -7, Now()))
    '    St = Format(St, "dd-MMM-yy")
    '    Dim Et As Date = Me.Et.Text
    '    Dim MyB As Boolean = False
    '    Dim Ud As Integer = 0
    '    Dim MyPinProcess As Boolean = False
    '    Try
    '        'If MyDBConnection.DBConnecting = True Then
    '        MySQLStr = "SELECT * from Delivery_sp where Marking is null or Marking ='1'"
    '        'MySQLStr = "SELECT wono, custno, item, Sum(oqty) AS SQTY ,shipdte FROM matran10 GROUP BY wono, custno, item, shipdte   HAVING  ( shipdte > #" & St & "# And shipdte < #" & Et & "# ) order by shipdte"
    '        ds = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
    '        If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
    '            For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
    '                MyID = ds.Tables(0).Rows(i)("Workorder") & "-" & ds.Tables(0).Rows(i)("PartNo")
    '                MyID = Trim(MyID)
    '                MyWO = Trim(ds.Tables(0).Rows(i)("Workorder"))
    '                Ud = CInt(ds.Tables(0).Rows(i)("UnitDelivery"))
    '                MyOUTPUT = GETOUTPUT(ds.Tables(0).Rows(i)("PartNo"))
    '                MyWQty = GETWQTY(MyWO, ds.Tables(0).Rows(i)("PartNo"), MyB, MyPinProcess)
    '                If MyB = False Then
    '                    MessageBox.Show("KHÔNG LẤY DỮ LIỆU APPAC DC. TẠM THỜI KO CẬP NHẬT")
    '                    Exit Function
    '                End If
    '                MySQLStr = "Select * from PDRemainQty where ID = '" & MyID & "'"
    '                ds1 = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
    '                If ds1.Tables.Count <> 0 AndAlso ds1.Tables(0).Rows.Count = 1 Then
    '                    If CInt(MyOUTPUT) > (CInt(MyWQty) + (MyWQty * PartTolerance)) AndAlso MyWO.Length > 3 AndAlso MyPinProcess = True Then
    '                        MySQLStr = "UPDATE PDRemainQty SET ActWIP = ActWIP + '" & Ud & "' where ID = '" & MyID & "'"
    '                        If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = False Then
    '                            MessageBox.Show("chua Cap nhat sl. tam ngung di")
    '                            Exit Function
    '                        End If
    '                    End If
    '                    MySQLStr = "UPDATE Delivery_sp SET Marking = '2'WHERE Workorder ='" & MyWO & "' and Deliverynumber ='" & ds.Tables(0).Rows(i)("Deliverynumber") & "' and PartNo ='" & ds.Tables(0).Rows(i)("PartNo") & "'"
    '                    If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
    '                        MessageBox.Show("Cap nhat marking ko dc. tam ngung di")
    '                        MyResult = False
    '                        Exit Function
    '                    End If
    '                Else
    '                    If CInt(MyOUTPUT) > (CInt(MyWQty) + (CInt(MyWQty) * PartTolerance)) AndAlso MyWO.Length > 3 AndAlso MyPinProcess = True Then
    '                        'ReQty = CInt(MyOUTPUT) - CInt(MyWQty)
    '                        ReQty = CInt(MyOUTPUT) - (MyWQty + (CInt(MyWQty) * PartTolerance))
    '                        MySQLStr = "INSERT INTO [PDRemainQty]([ID],[StockNo],[PartNo],[WO],[WQty],[Delivery],[ActWIP],[UpdateTime],[DeliveryNumber])" _
    '                        & " VALUES ('" & MyID & "','" & StockNo & "','" & ds.Tables(0).Rows(i)("partno") & "','" & ds.Tables(0).Rows(i)("workorder") & "'," & MyWQty & "," & Ud & ",'" & ReQty & "','" & Now() & "','" & ds.Tables(0).Rows(i)("Deliverynumber") & "')"
    '                        If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = True Then
    '                            MyResult = True
    '                        End If
    '                    End If
    '                    MySQLStr = "UPDATE Delivery_sp SET Marking = '2'WHERE Workorder ='" & MyWO & "' and Deliverynumber ='" & ds.Tables(0).Rows(i)("Deliverynumber") & "' and PartNo ='" & ds.Tables(0).Rows(i)("PartNo") & "'"
    '                    If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
    '                        MessageBox.Show("Cap nhat marking ko dc. tam ngung di")
    '                        MyResult = False
    '                    End If
    '                End If
    '            Next
    '            Me.lblTitle.BackColor = Color.Brown
    '            'MessageBox.Show("Da cap nhat xong ton PD")
    '        Else
    '            MyResult = False
    '            'MessageBox.Show("KO CO DU LIEU MOI DE CAP NHAT")
    '            Me.lblTitle.BackColor = Color.Purple
    '            ErrRecording("", "frmMain/BalUnitWoDelivery", "KO CO DU LIEU MOI DE CAP NHAT " & MySQLStr)
    '        End If
    '        'Else
    '        'MyResult = False
    '        'MessageBox.Show("KHÔNG KẾT NỐI SERVER ACCPAC CỤC BỘ.KIEM TRA LAI NHA")
    '        'End If
    '    Catch ex As Exception
    '        ErrRecording("", "frmMain/BalUnitWoDelivery", ex.Message)
    '        MessageBox.Show("LOI. BAO CAP TREN")
    '    Finally
    '        If MyDBConnection IsNot Nothing Then
    '            MyDBConnection.DBClosing()
    '        End If
    '    End Try
    '    Return MyResult
    'End Function

    Private Function GETWQTY(ByVal MyWO As String, ByVal MyP As String, ByRef MyB As Boolean, ByRef MyPinProcess As Boolean) As String
        Dim MyResult As String = 0
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        MyB = False
        MyPinProcess = False
        Try
            If MyDBConnection.DBConnecting = True Then
                MySQLStr = "SELECT wono, item, Sum(oqty) AS SQTY FROM matran10 GROUP BY wono, item   HAVING (((wono)='" & MyWO & "')) and item= '" & MyP & "'"
                ds = MyDBConnection.ExecuteProcedureDBSVR(MySQLStr)
                If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count = 1 Then
                    MyResult = ds.Tables(0).Rows(0)("SQTY")
                    MyPinProcess = True
                End If
                MyB = True
            Else
                MyB = False
                MessageBox.Show("KHÔNG KẾT NỐI SERVER ACCPAC CỤC BỘ.KIEM TRA LAI NHA")
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/GETWQTY", ex.Message)
            MyResult = 0
        Finally
            If MyDBConnection IsNot Nothing Then
                MyDBConnection.DBClosing()
            End If

        End Try
        Return MyResult
    End Function

    'Private Function BalUnitWoDelivery1() As Boolean
    '    Dim MyResult As Boolean = False
    '    Dim MySQLStr As String = ""
    '    Dim ds As DataSet
    '    Dim ds1 As DataSet
    '    Dim MyID As String = ""
    '    Dim MyOUTPUT As String = ""
    '    Dim ReQty As Integer = 0
    '    Dim St As Date = (DateAdd(DateInterval.Month, -7, Now()))
    '    St = Format(St, "dd-MMM-yy")
    '    Dim Et As Date = Me.Et.Text
    '    Try
    '        If MyDBConnection.DBConnecting = True Then
    '            MySQLStr = "SELECT wono, custno, item, Sum(oqty) AS SQTY ,shipdte FROM matran10 GROUP BY wono, custno, item, shipdte   HAVING  ( shipdte > #" & St & "# And shipdte < #" & Et & "# ) order by shipdte"
    '            ds = MyDBConnection.ExecuteProcedureDBSVR(MySQLStr)
    '            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
    '                For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
    '                    MyID = ds.Tables(0).Rows(i)("wono") & "-" & ds.Tables(0).Rows(i)("item")
    '                    MyID = Trim(MyID)
    '                    MyWO = Trim(ds.Tables(0).Rows(i)("wono"))
    '                    MyOUTPUT = GETOUTPUT(ds.Tables(0).Rows(i)("item"))
    '                    MySQLStr = "Select * from PDRemainQty where ID = '" & MyID & "'"
    '                    ds1 = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
    '                    If ds1.Tables.Count <> 0 AndAlso ds1.Tables(0).Rows.Count = 0 Then
    '                        If CInt(MyOUTPUT) > CInt(ds.Tables(0).Rows(i)("SQTY")) Then
    '                            ReQty = CInt(MyOUTPUT) - CInt(ds.Tables(0).Rows(i)("SQTY"))
    '                            MySQLStr = "INSERT INTO [PDRemainQty]([ID],[StockNo],[PartNo],[WO],[WQty],[Delivery],[ActWIP],[UpdateTime])" _
    '                                    & " VALUES ('" & MyID & "','" & StockNo & "','" & ds.Tables(0).Rows(i)("item") & "','" & ds.Tables(0).Rows(i)("wono") & "'," & ds.Tables(0).Rows(i)("SQTY") & "," & MyOUTPUT & ",'" & ReQty & "','" & Now() & "')"
    '                            If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = True Then
    '                                MyResult = True
    '                            End If
    '                        End If
    '                    End If
    '                Next
    '                MessageBox.Show("Da cap nhat xong")
    '            Else
    '                MyResult = False
    '                MessageBox.Show("WO NÀY TÌM ĐƯỢC DỮ LIỆU TRONG ACCPAC BẢNG MATRAN10")
    '                ErrRecording("", "frmMain/BalUnitWoDelivery", "WO NÀY TÌM ĐƯỢC DỮ LIỆU TRONG MATRAN10 " & MySQLStr)
    '            End If
    '        Else
    '            MyResult = False
    '            MessageBox.Show("KHÔNG KẾT NỐI SERVER ACCPAC CỤC BỘ.KIEM TRA LAI NHA")
    '        End If
    '    Catch ex As Exception
    '        ErrRecording("", "frmMain/BalUnitWoDelivery", ex.Message)
    '        MessageBox.Show("LOI. BAO CAP TREN")
    '    Finally
    '        If MyDBConnection IsNot Nothing Then
    '            MyDBConnection.DBClosing()
    '        End If
    '    End Try
    '    Return MyResult
    'End Function

    'Private Sub BalanceMoWOToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BalanceMoWOToolStripMenuItem1.Click
    '    MyDisplayFunctionFlg = "6"
    '    MyBMoWO = False
    '    BalanceMoWO()
    'End Sub

    'Private Sub BalanceLackMoWOToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BalanceLackMoWOToolStripMenuItem.Click
    '    MyDisplayFunctionFlg = "7"
    '    MyBMoWO = True
    '    BalanceMoWO()
    'End Sub

  

    Private Sub UpdatePDWIPToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdatePDWIPToolStripMenuItem.Click
        Me.lblTitle.BackColor = Color.Yellow
        'BalUnitWoDelivery()
    End Sub
    Private Sub cmdPrintLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrintLabel.Click
        dsObj.MainTable.Clear()
        frmPrintLocationLabel.Show()
    End Sub

    Private Sub cmdIDbarcode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdIDbarcode.Click
        dsObj.MainTable.Clear()
        If LoadIDBarcode_input() = True Then
            frmIDBarcode_Input.Show()
        Else
            MessageBox.Show("KHÔNG CÓ DỮ LIỆU IN ĐÂU. MỞ KO ĐƯỢC")
        End If
    End Sub
    Private Function LoadIDBarcode_input() As Boolean
        Dim MyResult As Boolean = False
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Dim ds1 As DataSet
        Dim ds2 As DataSet
        Try
            MySQLStr = "Select * from WH_PrintIDBarcode where PtintStatus ='1' and Code ='" & CurrentCode & "' ORDER BY ReadingTime"
            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                PIDRecord = ds.Tables(0).Rows.Count - 1
                For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                    MySQLStr = "Select * from tbInventory where IDRecord =" & ds.Tables(0).Rows(i)("IDRecord") & ""
                    ds1 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                    If ds1.Tables.Count <> 0 AndAlso ds1.Tables(0).Rows.Count = 1 Then
                        MySQLStr = "Select * from BarcodePrinter where IDRecord = " & ds.Tables(0).Rows(i)("IDRecord") & ""
                        ds2 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                        If ds2.Tables.Count <> 0 AndAlso ds2.Tables(0).Rows.Count = 1 Then
                            MyResult = True
                            dsObj.MainTable.AddMainTableRow(Trim(ds2.Tables(0).Rows(0)("IDRecord")), "*ID" & Trim(ds2.Tables(0).Rows(0)("IDRecord")) & "*", Trim(ds2.Tables(0).Rows(0)("PartNo")), "*" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "*", Trim(ds.Tables(0).Rows(i)("Shelf")), Trim(ds1.Tables(0).Rows(0)("Inventory")), "*" & CInt(Trim(ds1.Tables(0).Rows(0)("Inventory"))) & "*", Trim(ds2.Tables(0).Rows(0)("PONumber")), "*" & Trim(ds2.Tables(0).Rows(0)("PONumber")) & "*", Trim(ds2.Tables(0).Rows(0)("ReceiptDate")), (Trim(ds2.Tables(0).Rows(0)("TotalQty"))), Trim(ds.Tables(0).Rows(i)("Code")), "", "")
                        Else
                            MessageBox.Show("BarcodePrinter > 2")
                        End If
                    Else
                        MessageBox.Show(" tbInventory > 2")
                    End If
                Next
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString())
            ErrRecording("", "frmMain/LoadIDBarcode_input", ex.Message)
            MyResult = True
        End Try
        Return MyResult
    End Function


    Private Function PartReturnWH() As Boolean
        Dim MyResult As Boolean = False
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Dim ds1 As DataSet
        Dim ds2 As DataSet
        Dim ds3 As DataSet
        Dim ds4 As DataSet
        Dim MyPDes As String = ""
        Dim MyPUnit As String = ""
        Dim ReceiptNumber As String = ""
        Try
            MySQLStr = "Select * from WH_PartReturnWH where PtintStatus ='1' and Code ='" & CurrentCode & "' ORDER BY ReadingTime"
            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then


                ReceiptNumber = "PDRM " & Format(Now(), "yyMM")
                MySQLStr = "Select * from Receipt where substring(ReceiptNumber,1,9) = '" & ReceiptNumber & "' order by ReceiptNumber desc"
                ds4 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                If ds4.Tables.Count <> 0 AndAlso ds4.Tables(0).Rows.Count > 0 Then
                    ReceiptNumber = ReceiptNumber & " " & (CInt(Mid(ds4.Tables(0).Rows(0)("ReceiptNumber"), 11, 3)) + 1).ToString("000")
                Else
                    ReceiptNumber = ReceiptNumber & " 001"
                End If

                MySQLStr = "INSERT INTO [Receipt]([TransactionNumber],[ReceiptNumber],[StockNo],[SupplierNumber],[Receiver],[Deliverer],[R_Date],[MonthCurr],[lock],[bar_Lock],[SysDate],[Ck_Status])" _
                & " VALUES ('PDRM','" & ReceiptNumber & "','" & StockNo & "','PDRM','0004','0001','" & Format(Now(), "yyyy-MMM-dd") & "','" & Format(Now(), "MM/yyyy") & "',0,3,'" & Format(Now(), "yyyy-MMM-dd") & "',1)"
                If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
                    MessageBox.Show("chua Cap nhat Receipt. tam ngung di")
                    Exit Function
                End If

                PIDRecord1 = ds.Tables(0).Rows.Count - 1
                For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                    MySQLStr = "Select * from tbInventory where IDRecord =" & ds.Tables(0).Rows(i)("IDRecord") & ""
                    ds1 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                    If ds1.Tables.Count <> 0 AndAlso ds1.Tables(0).Rows.Count = 1 Then
                        MySQLStr = "Select * from BarcodePrinter where IDRecord = " & ds.Tables(0).Rows(i)("IDRecord") & ""
                        ds2 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                        If ds2.Tables.Count <> 0 AndAlso ds2.Tables(0).Rows.Count = 1 Then
                            MyResult = True
                            dsObj.MainTable.AddMainTableRow(Trim(ds2.Tables(0).Rows(0)("IDRecord")), "*ID" & Trim(ds2.Tables(0).Rows(0)("IDRecord")) & "*", Trim(ds2.Tables(0).Rows(0)("PartNo")), "*" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "*", Trim(ds.Tables(0).Rows(i)("Shelf")), CInt(Trim(ds1.Tables(0).Rows(0)("Inventory"))) + CInt(Trim(ds.Tables(0).Rows(i)("Qty"))), "*" & CInt(Trim(ds1.Tables(0).Rows(0)("Inventory"))) + CInt(Trim(ds.Tables(0).Rows(i)("Qty"))) & "*", Trim(ds2.Tables(0).Rows(0)("PONumber")), "*" & Trim(ds2.Tables(0).Rows(0)("PONumber")) & "*", Trim(ds2.Tables(0).Rows(0)("ReceiptDate")), CInt(Trim(ds2.Tables(0).Rows(0)("TotalQty"))), Trim(ds.Tables(0).Rows(i)("Code")), "", "")


                            'Dim MyID As String = Format(Now(), "yyMMddhhmmss")
                            'Dim ID As String = 99999 & "- " & Trim(ds2.Tables(0).Rows(0)("PartNo"))
                            'MySQLStr = "SELECT PartNo, Sum(ActWIP) AS SumOfActWIP FROM  PDRemainQty GROUP BY PDRemainQty.PartNo HAVING PartNo ='" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & " '  and Sum(ActWIP) > 0 "
                            ''MySQLStr = "SELECT * FROM PDRemainQty where PartNo ='" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "' and  ActWIP > 0"
                            'Dim dspd = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
                            'If dspd.Tables.Count <> 0 AndAlso dspd.Tables(0).Rows.Count > 0 Then
                            '    MySQLStr = "SELECT * FROM PDRemainQty where ID ='" & ID & "'"
                            '    Dim dsp = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
                            '    If dsp.Tables.Count <> 0 AndAlso dsp.Tables(0).Rows.Count = 0 Then
                            '        MySQLStr = "INSERT INTO [PDRemainQty]([ID],[StockNo],[PartNo],[WO],[WQty],[Delivery],[ActWIP],[UpdateTime])" _
                            '        & " VALUES ('" & MyID & i & "','" & StockNo & "','" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "','99999',99999," & CInt(Trim(ds.Tables(0).Rows(i)("Qty"))) & ",'" & -CInt(Trim(ds.Tables(0).Rows(i)("Qty"))) & "','" & Now() & "')"
                            '        If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = True Then
                            '            MyResult = True
                            '        Else
                            '            MessageBox.Show("loi")
                            '        End If
                            '    End If
                            'End If


                            MySQLStr = "UPDATE tbInventory SET Location = '" & ds.Tables(0).Rows(i)("Shelf") & "',Delivery = " & CInt(ds1.Tables(0).Rows(0)("Delivery")) - CInt(ds.Tables(0).Rows(i)("Qty")) & " WHERE IDRecord='" & ds.Tables(0).Rows(i)("IDRecord") & "'"
                            If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
                                MessageBox.Show("chua Cap nhat shelf. tam ngung di")
                                Exit Function
                            End If
                            MySQLStr = "UPDATE IDandLocation SET Location ='" & ds.Tables(0).Rows(i)("Shelf") & "'where ID ='" & ds.Tables(0).Rows(i)("IDRecord") & "'"
                            If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
                                MessageBox.Show("chua Cap nhat shelf. tam ngung di")
                                Exit Function
                            End If
                            MySQLStr = "UPDATE LOCATION SET LocationID = '" & ds.Tables(0).Rows(i)("Shelf") & "' WHERE IDRECORD='" & ds.Tables(0).Rows(i)("IDRecord") & "'"
                            If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
                                MessageBox.Show("chua Cap nhat shelf. tam ngung di")
                                Exit Function
                            End If

                            MySQLStr = "Select * from Receipt_Barcode where IDRecord =" & ds.Tables(0).Rows(i)("IDRecord") & ""
                            ds3 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                            If ds3.Tables.Count <> 0 AndAlso ds3.Tables(0).Rows.Count = 1 Then
                                MySQLStr = "UPDATE Receipt_Barcode SET Quantity = Quantity + " & ds.Tables(0).Rows(i)("Qty") & ", OldQty = '" & ds3.Tables(0).Rows(0)("Quantity") & "-" & ds.Tables(0).Rows(i)("Qty") & "' WHERE IDRECORD='" & ds.Tables(0).Rows(i)("IDRecord") & "'"
                                If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
                                    MessageBox.Show("chua Cap nhat Receipt_Barcode. tam ngung di")
                                    Exit Function
                                End If
                            End If
                            'ReceiptNumber = "PDRM " & Format(Now(), "yyMM")
                            'MySQLStr = "Select * from Receipt where substring(ReceiptNumber,1,9) = '" & ReceiptNumber & "' order by ReceiptNumber desc"
                            'ds4 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                            'If ds4.Tables.Count <> 0 AndAlso ds4.Tables(0).Rows.Count > 0 Then
                            '    ReceiptNumber = ReceiptNumber & " " & (CInt(Mid(ds4.Tables(0).Rows(0)("ReceiptNumber"), 11, 3)) + 1).ToString("000")
                            'Else
                            '    ReceiptNumber = ReceiptNumber & " 001"
                            'End If

                            'MySQLStr = "INSERT INTO [Receipt]([TransactionNumber],[ReceiptNumber],[StockNo],[SupplierNumber],[Receiver],[Deliverer],[R_Date],[MonthCurr],[lock],[bar_Lock],[SysDate],[Ck_Status])" _
                            '& " VALUES ('PDRM','" & ReceiptNumber & "','" & StockNo & "','PDRM','0004','0001','" & Format(Now(), "yyyy-MMM-dd") & "','" & Format(Now(), "MM/yyyy") & "',0,3,'" & Format(Now(), "yyyy-MMM-dd") & "',1)"
                            'If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
                            '    MessageBox.Show("chua Cap nhat Receipt. tam ngung di")
                            '    Exit Function
                            'End If

                            MySQLStr = "Select * from Receipt_sp where POnumber ='" & ds2.Tables(0).Rows(0)("PONumber") & "'  and ReceiptNumber ='" & ReceiptNumber & "' and PartNo ='" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "'"
                            ds1 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                            If ds1.Tables.Count <> 0 AndAlso ds1.Tables(0).Rows.Count = 1 Then
                                MySQLStr = "UPDATE Receipt_sp SET [UnitPO] = [UnitPO]  + " & ds.Tables(0).Rows(i)("Qty") & ", [UnitOrder]= [UnitOrder]+ " & ds.Tables(0).Rows(i)("Qty") & ", [UnitReceipt]= [UnitReceipt]+ " & ds.Tables(0).Rows(i)("Qty") & "  where POnumber ='" & ds2.Tables(0).Rows(0)("PONumber") & "'  and ReceiptNumber ='" & ReceiptNumber & "' and PartNo ='" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "'"
                                If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
                                    MessageBox.Show("chua Cap nhat da Delivery_sp . tam ngung di")
                                    Exit Function
                                End If
                            Else
                                MySQLStr = "INSERT INTO [Receipt_sp]([ReceiptNumber],[POnumber],[DateExpected],[PartNo],[UnitPO],[UnitOrder],[UnitReceipt],[POnumberLines])" _
                              & " VALUES ('" & ReceiptNumber & "','" & Trim(ds2.Tables(0).Rows(0)("PONumber")) & "','" & Format(Now(), "yyyy-MMM-dd") & "','" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "'," & ds.Tables(0).Rows(i)("Qty") & "," & ds.Tables(0).Rows(i)("Qty") & "," & ds.Tables(0).Rows(i)("Qty") & ",'1')"
                                If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
                                    MessageBox.Show("chua Cap nhat Receipt_sp. tam ngung di")
                                    Exit Function
                                End If
                            End If

                            MySQLStr = "UPDATE WH_PartReturnWH SET PtintStatus ='2', ReceiptNote = '" & ReceiptNumber & "', PO= '" & Trim(ds2.Tables(0).Rows(0)("PONumber")) & "', PartName ='" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "'where ID ='" & ds.Tables(0).Rows(i)("ID") & "'"
                            If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = False Then
                                MessageBox.Show("chua Cap nhat da in nhan. tam ngung di")
                                Exit Function
                            End If

                        Else
                            MessageBox.Show("BarcodePrinter > 2")
                        End If
                    Else
                        MessageBox.Show(" tbInventory > 2")
                    End If
                Next

                '    MySQLStr = "SELECT PartName, PO, sum(Qty) as Qty, Code, ReceiptNote  FROM WH_PartReturnWH GROUP BY PartName, PO,  Code, HAVING PtintStatus = '2' ORDER BY PartName"

                MySQLStr = "SELECT PartName, PO, Sum(Qty) AS Qty, Code, PtintStatus, ReceiptNote FROM WH_PartReturnWH GROUP BY PartName, PO, Code, PtintStatus, ReceiptNote HAVING PtintStatus = '2' AND ReceiptNote = '" & ReceiptNumber & "'"
                'MySQLStr = "SELECT PartName, PO, Sum(Qty) AS Qty, Code, PtintStatus, ReceiptNote FROM WH_PartReturnWH GROUP BY PartName, PO, Code, PtintStatus, ReceiptNote HAVING PtintStatus)= '2' AND ReceiptNote = '" & ReceiptNumber & "'"
                ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
                If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                    dsSubObj.MainTable.Clear()
                    For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                        MyResult = True
                        GetWHDES(Trim(ds.Tables(0).Rows(i)("PartName")), MyPDes, MyPUnit)
                        dsSubObj.MainTable.AddMainTableRow(i + 1, ds.Tables(0).Rows(i)("PartName"), "", ds.Tables(0).Rows(i)("PO"), MyPUnit, "PD return Material", ds.Tables(0).Rows(i)("Qty"), ds.Tables(0).Rows(i)("Qty"), "", CurrentUserName, "WH_01", "", "", ds.Tables(0).Rows(i)("ReceiptNote"))
                    Next
                End If


            Else
                MySQLStr = "Select * from WH_PartReturnWH where PtintStatus ='2'  and ( PtintNote ='1' or PtintNote is null )   and Code ='" & CurrentCode & "' ORDER BY ReadingTime"
                ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
                If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                    For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                        PIDRecord1 = ds.Tables(0).Rows.Count - 1
                        MySQLStr = "Select * from tbInventory where IDRecord =" & ds.Tables(0).Rows(i)("IDRecord") & ""
                        ds1 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                        If ds1.Tables.Count <> 0 AndAlso ds1.Tables(0).Rows.Count = 1 Then
                            MySQLStr = "Select * from BarcodePrinter where IDRecord = " & ds.Tables(0).Rows(i)("IDRecord") & ""
                            ds2 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                            If ds2.Tables.Count <> 0 AndAlso ds2.Tables(0).Rows.Count = 1 Then
                                MyResult = True
                                dsObj.MainTable.AddMainTableRow(Trim(ds2.Tables(0).Rows(0)("IDRecord")), "*ID" & Trim(ds2.Tables(0).Rows(0)("IDRecord")) & "*", Trim(ds2.Tables(0).Rows(0)("PartNo")), "*" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "*", Trim(ds.Tables(0).Rows(i)("Shelf")), CInt(Trim(ds1.Tables(0).Rows(0)("Inventory"))), "*" & CInt(Trim(ds1.Tables(0).Rows(0)("Inventory"))) & "*", Trim(ds2.Tables(0).Rows(0)("PONumber")), "*" & Trim(ds2.Tables(0).Rows(0)("PONumber")) & "*", Trim(ds2.Tables(0).Rows(0)("ReceiptDate")), CInt(Trim(ds2.Tables(0).Rows(0)("TotalQty"))), Trim(ds.Tables(0).Rows(i)("Code")), "", "")
                            Else
                                MessageBox.Show("BarcodePrinter > 2")
                            End If
                        Else
                            MessageBox.Show(" tbInventory > 2")
                        End If
                    Next
                    MySQLStr = "SELECT PartName, PO, Sum(Qty) AS Qty, Code, PtintStatus, ReceiptNote FROM WH_PartReturnWH GROUP BY PartName, PO, Code, PtintStatus, ReceiptNote HAVING PtintStatus = '2' AND ReceiptNote = '" & ds.Tables(0).Rows(0)("ReceiptNote") & "'"
                    ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
                    If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                        dsSubObj.MainTable.Clear()
                        For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                            MyResult = True
                            GetWHDES(Trim(ds.Tables(0).Rows(i)("PartName")), MyPDes, MyPUnit)
                            dsSubObj.MainTable.AddMainTableRow(i + 1, ds.Tables(0).Rows(i)("PartName"), "", ds.Tables(0).Rows(i)("PO"), MyPUnit, "PD return Material", ds.Tables(0).Rows(i)("Qty"), ds.Tables(0).Rows(i)("Qty"), "", CurrentUserName, "WH_01", "", "", ds.Tables(0).Rows(i)("ReceiptNote"))
                        Next
                    End If

                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString())
            ErrRecording("", "frmMain/PartReturnWH", ex.Message)
            MyResult = False
        End Try
        Return MyResult
    End Function

    Private Function PDReturnMaterial() As Boolean
        Dim MyResult As Boolean = False
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Dim ds1 As DataSet
        Dim ds2 As DataSet
        Dim ds3 As DataSet
        Dim ds4 As DataSet
        Dim MyPDes As String = ""
        Dim MyPUnit As String = ""
        Dim ReceiptNumber As String = ""
      
       
        Try
            MySQLStr = "Select * from WH_PDReturnMaterial where PtintStatus ='1' and Code ='" & CurrentCode & "' ORDER BY ReadingTime"
            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                Dim DEPT As String = InputBox("Nhập bộ nhân trả hàng về kho")
                DEPT = UCase(DEPT)
                If DEPT = "PD" OrElse DEPT = "WH" OrElse DEPT = "QA" OrElse DEPT = "ME" OrElse DEPT = "EG" OrElse DEPT = "OD" Then
                    DEPT = DEPT & "RM"
                Else
                    MessageBox.Show("Không đúng. Yêu cầu : Kho nhập là WH hoặc QA hoặc PD hoặc ME hoặc EG hoặc ko xác định bộ phận nhập OD ")
                    MyResult = False
                    Exit Function
                End If
                ReceiptNumber = DEPT & " " & Format(Now(), "yyMM")
                MySQLStr = "Select * from Receipt where substring(ReceiptNumber,1,9) = '" & ReceiptNumber & "' order by ReceiptNumber desc"
                ds4 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                If ds4.Tables.Count <> 0 AndAlso ds4.Tables(0).Rows.Count > 0 Then
                    ReceiptNumber = ReceiptNumber & " " & (CInt(Mid(ds4.Tables(0).Rows(0)("ReceiptNumber"), 11, 3)) + 1).ToString("000")
                Else
                    ReceiptNumber = ReceiptNumber & " 001"
                End If

                MySQLStr = "INSERT INTO [Receipt]([TransactionNumber],[ReceiptNumber],[StockNo],[SupplierNumber],[Receiver],[Deliverer],[R_Date],[MonthCurr],[lock],[bar_Lock],[SysDate],[Ck_Status])" _
                & " VALUES ('" & DEPT & "','" & ReceiptNumber & "','" & StockNo & "','" & DEPT & "','0004','0001','" & Format(Now(), "yyyy-MMM-dd") & "','" & Format(Now(), "MM/yyyy") & "',0,3,'" & Format(Now(), "yyyy-MMM-dd") & "',1)"
                If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
                    MessageBox.Show("chua Cap nhat Receipt. tam ngung di")
                    Exit Function
                End If

                PIDRecord1 = ds.Tables(0).Rows.Count - 1
                For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                    MySQLStr = "Select * from tbInventory where IDRecord =" & ds.Tables(0).Rows(i)("IDRecord") & ""
                    ds1 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                    If ds1.Tables.Count <> 0 AndAlso ds1.Tables(0).Rows.Count = 1 Then
                        MySQLStr = "Select * from BarcodePrinter where IDRecord = " & ds.Tables(0).Rows(i)("IDRecord") & ""
                        ds2 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                        If ds2.Tables.Count <> 0 AndAlso ds2.Tables(0).Rows.Count = 1 Then
                            MyResult = True
                            dsObj.MainTable.AddMainTableRow(Trim(ds2.Tables(0).Rows(0)("IDRecord")), "*ID" & Trim(ds2.Tables(0).Rows(0)("IDRecord")) & "*", Trim(ds2.Tables(0).Rows(0)("PartNo")), "*" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "*", Trim(ds.Tables(0).Rows(i)("Shelf")), CLng(Trim(ds1.Tables(0).Rows(0)("Inventory"))) + CLng(Trim(ds.Tables(0).Rows(i)("Qty"))), "*" & CLng(Trim(ds1.Tables(0).Rows(0)("Inventory"))) + CLng(Trim(ds.Tables(0).Rows(i)("Qty"))) & "*", Trim(ds2.Tables(0).Rows(0)("PONumber")), "*" & Trim(ds2.Tables(0).Rows(0)("PONumber")) & "*", Trim(ds2.Tables(0).Rows(0)("ReceiptDate")), (Trim(ds2.Tables(0).Rows(0)("TotalQty"))), Trim(ds.Tables(0).Rows(i)("Code")), "", "")


                            'Dim MyID As String = Format(Now(), "yyMMddhhmmss")
                            'Dim ID As String = 99999 & "- " & Trim(ds2.Tables(0).Rows(0)("PartNo"))
                            'MySQLStr = "SELECT PartNo, Sum(ActWIP) AS SumOfActWIP FROM  PDRemainQty GROUP BY PDRemainQty.PartNo HAVING PartNo ='" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & " '  and Sum(ActWIP) > 0 "
                            ''MySQLStr = "SELECT * FROM PDRemainQty where PartNo ='" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "' and  ActWIP > 0"
                            'Dim dspd = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
                            'If dspd.Tables.Count <> 0 AndAlso dspd.Tables(0).Rows.Count > 0 Then
                            '    MySQLStr = "SELECT * FROM PDRemainQty where ID ='" & ID & "'"
                            '    Dim dsp = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
                            '    If dsp.Tables.Count <> 0 AndAlso dsp.Tables(0).Rows.Count = 0 Then
                            '        MySQLStr = "INSERT INTO [PDRemainQty]([ID],[StockNo],[PartNo],[WO],[WQty],[Delivery],[ActWIP],[UpdateTime])" _
                            '        & " VALUES ('" & MyID & i & "','" & StockNo & "','" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "','99999',99999," & CInt(Trim(ds.Tables(0).Rows(i)("Qty"))) & ",'" & -CInt(Trim(ds.Tables(0).Rows(i)("Qty"))) & "','" & Now() & "')"
                            '        If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = True Then
                            '            MyResult = True
                            '        Else
                            '            MessageBox.Show("loi")
                            '        End If
                            '    End If
                            'End If


                            MySQLStr = "UPDATE tbInventory SET Location = '" & ds.Tables(0).Rows(i)("Shelf") & "',Delivery = " & CInt(ds1.Tables(0).Rows(0)("Delivery")) - CInt(ds.Tables(0).Rows(i)("Qty")) & " WHERE IDRecord='" & ds.Tables(0).Rows(i)("IDRecord") & "'"
                            If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
                                MessageBox.Show("chua Cap nhat shelf. tam ngung di")
                                Exit Function
                            End If
                            MySQLStr = "UPDATE IDandLocation SET Location ='" & ds.Tables(0).Rows(i)("Shelf") & "'where ID ='" & ds.Tables(0).Rows(i)("IDRecord") & "'"
                            If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
                                MessageBox.Show("chua Cap nhat shelf. tam ngung di")
                                Exit Function
                            End If
                            MySQLStr = "UPDATE LOCATION SET LocationID = '" & ds.Tables(0).Rows(i)("Shelf") & "' WHERE IDRECORD='" & ds.Tables(0).Rows(i)("IDRecord") & "'"
                            If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
                                MessageBox.Show("chua Cap nhat shelf. tam ngung di")
                                Exit Function
                            End If

                            MySQLStr = "Select * from Receipt_Barcode where IDRecord =" & ds.Tables(0).Rows(i)("IDRecord") & ""
                            ds3 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                            If ds3.Tables.Count <> 0 AndAlso ds3.Tables(0).Rows.Count = 1 Then
                                MySQLStr = "UPDATE Receipt_Barcode SET Quantity = Quantity + " & ds.Tables(0).Rows(i)("Qty") & ", OldQty = '" & ds3.Tables(0).Rows(0)("Quantity") & "-" & ds.Tables(0).Rows(i)("Qty") & "' WHERE IDRECORD='" & ds.Tables(0).Rows(i)("IDRecord") & "'"
                                If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
                                    MessageBox.Show("chua Cap nhat Receipt_Barcode. tam ngung di")
                                    Exit Function
                                End If
                            End If
                            'ReceiptNumber = "PDRM " & Format(Now(), "yyMM")
                            'MySQLStr = "Select * from Receipt where substring(ReceiptNumber,1,9) = '" & ReceiptNumber & "' order by ReceiptNumber desc"
                            'ds4 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                            'If ds4.Tables.Count <> 0 AndAlso ds4.Tables(0).Rows.Count > 0 Then
                            '    ReceiptNumber = ReceiptNumber & " " & (CInt(Mid(ds4.Tables(0).Rows(0)("ReceiptNumber"), 11, 3)) + 1).ToString("000")
                            'Else
                            '    ReceiptNumber = ReceiptNumber & " 001"
                            'End If

                            'MySQLStr = "INSERT INTO [Receipt]([TransactionNumber],[ReceiptNumber],[StockNo],[SupplierNumber],[Receiver],[Deliverer],[R_Date],[MonthCurr],[lock],[bar_Lock],[SysDate],[Ck_Status])" _
                            '& " VALUES ('PDRM','" & ReceiptNumber & "','" & StockNo & "','PDRM','0004','0001','" & Format(Now(), "yyyy-MMM-dd") & "','" & Format(Now(), "MM/yyyy") & "',0,3,'" & Format(Now(), "yyyy-MMM-dd") & "',1)"
                            'If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
                            '    MessageBox.Show("chua Cap nhat Receipt. tam ngung di")
                            '    Exit Function
                            'End If

                            MySQLStr = "Select * from Receipt_sp where POnumber ='" & ds2.Tables(0).Rows(0)("PONumber") & "'  and ReceiptNumber ='" & ReceiptNumber & "' and PartNo ='" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "'"
                            ds1 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                            If ds1.Tables.Count <> 0 AndAlso ds1.Tables(0).Rows.Count = 1 Then
                                MySQLStr = "UPDATE Receipt_sp SET [UnitPO] = [UnitPO]  + " & ds.Tables(0).Rows(i)("Qty") & ", [UnitOrder]= [UnitOrder]+ " & ds.Tables(0).Rows(i)("Qty") & ", [UnitReceipt]= [UnitReceipt]+ " & ds.Tables(0).Rows(i)("Qty") & "  where POnumber ='" & ds2.Tables(0).Rows(0)("PONumber") & "'  and ReceiptNumber ='" & ReceiptNumber & "' and PartNo ='" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "'"
                                If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
                                    MessageBox.Show("chua Cap nhat da Delivery_sp . tam ngung di")
                                    Exit Function
                                End If
                            Else
                                MySQLStr = "INSERT INTO [Receipt_sp]([ReceiptNumber],[POnumber],[DateExpected],[PartNo],[UnitPO],[UnitOrder],[UnitReceipt],[POnumberLines])" _
                              & " VALUES ('" & ReceiptNumber & "','" & Trim(ds2.Tables(0).Rows(0)("PONumber")) & "','" & Format(Now(), "yyyy-MMM-dd") & "','" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "'," & ds.Tables(0).Rows(i)("Qty") & "," & ds.Tables(0).Rows(i)("Qty") & "," & ds.Tables(0).Rows(i)("Qty") & ",'1')"
                                If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
                                    MessageBox.Show("chua Cap nhat Receipt_sp. tam ngung di")
                                    Exit Function
                                End If
                            End If

                            MySQLStr = "UPDATE WH_PDReturnMaterial SET PtintStatus ='2', ReceiptNote = '" & ReceiptNumber & "', PO= '" & Trim(ds2.Tables(0).Rows(0)("PONumber")) & "', PartName ='" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "'where ID ='" & ds.Tables(0).Rows(i)("ID") & "'"
                            If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = False Then
                                MessageBox.Show("chua Cap nhat da in nhan. tam ngung di")
                                Exit Function
                            End If

                        Else
                            MessageBox.Show("BarcodePrinter > 2")
                        End If
                    Else
                        MessageBox.Show(" tbInventory > 2")
                    End If
                Next

                '    MySQLStr = "SELECT PartName, PO, sum(Qty) as Qty, Code, ReceiptNote  FROM WH_PDReturnMaterial GROUP BY PartName, PO,  Code, HAVING PtintStatus = '2' ORDER BY PartName"

                MySQLStr = "SELECT PartName, PO, Sum(Qty) AS Qty, Code, PtintStatus, ReceiptNote FROM WH_PDReturnMaterial GROUP BY PartName, PO, Code, PtintStatus, ReceiptNote HAVING PtintStatus = '2' AND ReceiptNote = '" & ReceiptNumber & "'"
                'MySQLStr = "SELECT PartName, PO, Sum(Qty) AS Qty, Code, PtintStatus, ReceiptNote FROM WH_PDReturnMaterial GROUP BY PartName, PO, Code, PtintStatus, ReceiptNote HAVING PtintStatus)= '2' AND ReceiptNote = '" & ReceiptNumber & "'"
                ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
                If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                    dsSubObj.MainTable.Clear()
                    For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                        MyResult = True
                        GetWHDES(Trim(ds.Tables(0).Rows(i)("PartName")), MyPDes, MyPUnit)
                        dsSubObj.MainTable.AddMainTableRow(i + 1, ds.Tables(0).Rows(i)("PartName"), "", ds.Tables(0).Rows(i)("PO"), MyPUnit, "PD return Material", ds.Tables(0).Rows(i)("Qty"), ds.Tables(0).Rows(i)("Qty"), "", CurrentUserName, "WH_01", "", "", ds.Tables(0).Rows(i)("ReceiptNote"))
                    Next
                End If


            Else
                MySQLStr = "Select * from WH_PDReturnMaterial where PtintStatus ='2'  and ( PtintNote ='1' or PtintNote is null )   and Code ='" & CurrentCode & "' ORDER BY ReadingTime"
                ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
                If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                    For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                        PIDRecord1 = ds.Tables(0).Rows.Count - 1
                        MySQLStr = "Select * from tbInventory where IDRecord =" & ds.Tables(0).Rows(i)("IDRecord") & ""
                        ds1 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                        If ds1.Tables.Count <> 0 AndAlso ds1.Tables(0).Rows.Count = 1 Then
                            MySQLStr = "Select * from BarcodePrinter where IDRecord = " & ds.Tables(0).Rows(i)("IDRecord") & ""
                            ds2 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                            If ds2.Tables.Count <> 0 AndAlso ds2.Tables(0).Rows.Count = 1 Then
                                MyResult = True
                                dsObj.MainTable.AddMainTableRow(Trim(ds2.Tables(0).Rows(0)("IDRecord")), "*ID" & Trim(ds2.Tables(0).Rows(0)("IDRecord")) & "*", Trim(ds2.Tables(0).Rows(0)("PartNo")), "*" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "*", Trim(ds.Tables(0).Rows(i)("Shelf")), CInt(Trim(ds1.Tables(0).Rows(0)("Inventory"))), "*" & (Trim(ds1.Tables(0).Rows(0)("Inventory"))) & "*", Trim(ds2.Tables(0).Rows(0)("PONumber")), "*" & Trim(ds2.Tables(0).Rows(0)("PONumber")) & "*", Trim(ds2.Tables(0).Rows(0)("ReceiptDate")), CInt(Trim(ds2.Tables(0).Rows(0)("TotalQty"))), Trim(ds.Tables(0).Rows(i)("Code")), "", "")
                            Else
                                MessageBox.Show("BarcodePrinter > 2")
                            End If
                        Else
                            MessageBox.Show(" tbInventory > 2")
                        End If
                    Next
                    MySQLStr = "SELECT PartName, PO, Sum(Qty) AS Qty, Code, PtintStatus, ReceiptNote FROM WH_PDReturnMaterial GROUP BY PartName, PO, Code, PtintStatus, ReceiptNote HAVING PtintStatus = '2' AND ReceiptNote = '" & ds.Tables(0).Rows(0)("ReceiptNote") & "'"
                    ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
                    If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                        dsSubObj.MainTable.Clear()
                        For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                            MyResult = True
                            GetWHDES(Trim(ds.Tables(0).Rows(i)("PartName")), MyPDes, MyPUnit)
                            dsSubObj.MainTable.AddMainTableRow(i + 1, ds.Tables(0).Rows(i)("PartName"), "", ds.Tables(0).Rows(i)("PO"), MyPUnit, "PD return Material", ds.Tables(0).Rows(i)("Qty"), ds.Tables(0).Rows(i)("Qty"), "", CurrentUserName, "WH_01", "", "", ds.Tables(0).Rows(i)("ReceiptNote"))
                        Next
                    End If

                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString())
            ErrRecording("", "frmMain/PDReturnMaterial", ex.Message)
            MyResult = False
        End Try
        Return MyResult
    End Function

    'Private Function PDReturnMaterial() As Boolean
    '    Dim MyResult As Boolean = False
    '    Dim MySQLStr As String = ""
    '    Dim ds As DataSet
    '    Dim ds1 As DataSet
    '    Dim ds2 As DataSet
    '    Dim ds3 As DataSet
    '    Dim ds4 As DataSet
    '    Dim MyPDes As String = ""
    '    Dim MyPUnit As String = ""
    '    Dim ReceiptNumber As String = ""
    '    Try
    '        MySQLStr = "Select * from WH_PDReturnMaterial where PtintStatus ='1' and Code ='" & CurrentCode & "' ORDER BY ReadingTime"
    '        ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
    '        If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then


    '            ReceiptNumber = "PDRM " & Format(Now(), "yyMM")
    '            MySQLStr = "Select * from Receipt where substring(ReceiptNumber,1,9) = '" & ReceiptNumber & "' order by ReceiptNumber desc"
    '            ds4 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
    '            If ds4.Tables.Count <> 0 AndAlso ds4.Tables(0).Rows.Count > 0 Then
    '                ReceiptNumber = ReceiptNumber & " " & (CInt(Mid(ds4.Tables(0).Rows(0)("ReceiptNumber"), 11, 3)) + 1).ToString("000")
    '            Else
    '                ReceiptNumber = ReceiptNumber & " 001"
    '            End If

    '            MySQLStr = "INSERT INTO [Receipt]([TransactionNumber],[ReceiptNumber],[StockNo],[SupplierNumber],[Receiver],[Deliverer],[R_Date],[MonthCurr],[lock],[bar_Lock],[SysDate],[Ck_Status])" _
    '            & " VALUES ('PDRM','" & ReceiptNumber & "','" & StockNo & "','PDRM','0004','0001','" & Format(Now(), "yyyy-MMM-dd") & "','" & Format(Now(), "MM/yyyy") & "',0,3,'" & Format(Now(), "yyyy-MMM-dd") & "',1)"
    '            If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
    '                MessageBox.Show("chua Cap nhat Receipt. tam ngung di")
    '                Exit Function
    '            End If

    '            PIDRecord1 = ds.Tables(0).Rows.Count - 1
    '            For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
    '                MySQLStr = "Select * from tbInventory where IDRecord =" & ds.Tables(0).Rows(i)("IDRecord") & ""
    '                ds1 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
    '                If ds1.Tables.Count <> 0 AndAlso ds1.Tables(0).Rows.Count = 1 Then
    '                    MySQLStr = "Select * from BarcodePrinter where IDRecord = " & ds.Tables(0).Rows(i)("IDRecord") & ""
    '                    ds2 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
    '                    If ds2.Tables.Count <> 0 AndAlso ds2.Tables(0).Rows.Count = 1 Then
    '                        MyResult = True
    '                        dsObj.MainTable.AddMainTableRow(Trim(ds2.Tables(0).Rows(0)("IDRecord")), "*ID" & Trim(ds2.Tables(0).Rows(0)("IDRecord")) & "*", Trim(ds2.Tables(0).Rows(0)("PartNo")), "*" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "*", Trim(ds.Tables(0).Rows(i)("Shelf")), CInt(Trim(ds1.Tables(0).Rows(0)("Inventory"))) + CInt(Trim(ds.Tables(0).Rows(i)("Qty"))), "*" & CInt(Trim(ds1.Tables(0).Rows(0)("Inventory"))) + CInt(Trim(ds.Tables(0).Rows(i)("Qty"))) & "*", Trim(ds2.Tables(0).Rows(0)("PONumber")), "*" & Trim(ds2.Tables(0).Rows(0)("PONumber")) & "*", Trim(ds2.Tables(0).Rows(0)("ReceiptDate")), CInt(Trim(ds2.Tables(0).Rows(0)("TotalQty"))), Trim(ds.Tables(0).Rows(i)("Code")), "", "")


    '                        Dim MyID As String = Format(Now(), "yyMMddhhmmss")
    '                        Dim ID As String = 99999 & "- " & Trim(ds2.Tables(0).Rows(0)("PartNo"))
    '                        MySQLStr = "SELECT PartNo, Sum(ActWIP) AS SumOfActWIP FROM  PDRemainQty GROUP BY PDRemainQty.PartNo HAVING PartNo ='" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & " '  and Sum(ActWIP) > 0 "
    '                        'MySQLStr = "SELECT * FROM PDRemainQty where PartNo ='" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "' and  ActWIP > 0"
    '                        Dim dspd = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
    '                        If dspd.Tables.Count <> 0 AndAlso dspd.Tables(0).Rows.Count > 0 Then
    '                            MySQLStr = "SELECT * FROM PDRemainQty where ID ='" & ID & "'"
    '                            Dim dsp = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
    '                            If dsp.Tables.Count <> 0 AndAlso dsp.Tables(0).Rows.Count = 0 Then
    '                                MySQLStr = "INSERT INTO [PDRemainQty]([ID],[StockNo],[PartNo],[WO],[WQty],[Delivery],[ActWIP],[UpdateTime])" _
    '                                & " VALUES ('" & MyID & i & "','" & StockNo & "','" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "','99999',99999," & CInt(Trim(ds.Tables(0).Rows(i)("Qty"))) & ",'" & -CInt(Trim(ds.Tables(0).Rows(i)("Qty"))) & "','" & Now() & "')"
    '                                If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = True Then
    '                                    MyResult = True
    '                                Else
    '                                    MessageBox.Show("loi")
    '                                End If
    '                            End If
    '                        End If


    '                        MySQLStr = "UPDATE tbInventory SET Location = '" & ds.Tables(0).Rows(i)("Shelf") & "',Delivery = " & CInt(ds1.Tables(0).Rows(0)("Delivery")) - CInt(ds.Tables(0).Rows(i)("Qty")) & " WHERE IDRecord='" & ds.Tables(0).Rows(i)("IDRecord") & "'"
    '                        If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
    '                            MessageBox.Show("chua Cap nhat shelf. tam ngung di")
    '                            Exit Function
    '                        End If
    '                        MySQLStr = "UPDATE IDandLocation SET Location ='" & ds.Tables(0).Rows(i)("Shelf") & "'where ID ='" & ds.Tables(0).Rows(i)("IDRecord") & "'"
    '                        If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
    '                            MessageBox.Show("chua Cap nhat shelf. tam ngung di")
    '                            Exit Function
    '                        End If
    '                        MySQLStr = "UPDATE LOCATION SET LocationID = '" & ds.Tables(0).Rows(i)("Shelf") & "' WHERE IDRECORD='" & ds.Tables(0).Rows(i)("IDRecord") & "'"
    '                        If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
    '                            MessageBox.Show("chua Cap nhat shelf. tam ngung di")
    '                            Exit Function
    '                        End If

    '                        MySQLStr = "Select * from Receipt_Barcode where IDRecord =" & ds.Tables(0).Rows(i)("IDRecord") & ""
    '                        ds3 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
    '                        If ds3.Tables.Count <> 0 AndAlso ds3.Tables(0).Rows.Count = 1 Then
    '                            MySQLStr = "UPDATE Receipt_Barcode SET Quantity = Quantity + " & ds.Tables(0).Rows(i)("Qty") & ", OldQty = '" & ds3.Tables(0).Rows(0)("Quantity") & "-" & ds.Tables(0).Rows(i)("Qty") & "' WHERE IDRECORD='" & ds.Tables(0).Rows(i)("IDRecord") & "'"
    '                            If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
    '                                MessageBox.Show("chua Cap nhat Receipt_Barcode. tam ngung di")
    '                                Exit Function
    '                            End If
    '                        End If
    '                        'ReceiptNumber = "PDRM " & Format(Now(), "yyMM")
    '                        'MySQLStr = "Select * from Receipt where substring(ReceiptNumber,1,9) = '" & ReceiptNumber & "' order by ReceiptNumber desc"
    '                        'ds4 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
    '                        'If ds4.Tables.Count <> 0 AndAlso ds4.Tables(0).Rows.Count > 0 Then
    '                        '    ReceiptNumber = ReceiptNumber & " " & (CInt(Mid(ds4.Tables(0).Rows(0)("ReceiptNumber"), 11, 3)) + 1).ToString("000")
    '                        'Else
    '                        '    ReceiptNumber = ReceiptNumber & " 001"
    '                        'End If

    '                        'MySQLStr = "INSERT INTO [Receipt]([TransactionNumber],[ReceiptNumber],[StockNo],[SupplierNumber],[Receiver],[Deliverer],[R_Date],[MonthCurr],[lock],[bar_Lock],[SysDate],[Ck_Status])" _
    '                        '& " VALUES ('PDRM','" & ReceiptNumber & "','" & StockNo & "','PDRM','0004','0001','" & Format(Now(), "yyyy-MMM-dd") & "','" & Format(Now(), "MM/yyyy") & "',0,3,'" & Format(Now(), "yyyy-MMM-dd") & "',1)"
    '                        'If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
    '                        '    MessageBox.Show("chua Cap nhat Receipt. tam ngung di")
    '                        '    Exit Function
    '                        'End If

    '                        MySQLStr = "Select * from Receipt_sp where UnitPO ='" & ds2.Tables(0).Rows(0)("PONumber") & "'  and ReceiptNumber ='" & ReceiptNumber & "' and PartNo ='" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "'"
    '                        ds1 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
    '                        If ds1.Tables.Count <> 0 AndAlso ds1.Tables(0).Rows.Count = 1 Then
    '                            MySQLStr = "UPDATE ReceiptNumber SET [UnitPO] = [UnitPO]  + " & ds.Tables(0).Rows(i)("DeliveryQty") & ", [UnitOrder]= [UnitOrder]+ " & ds.Tables(0).Rows(i)("Qty") & ", [UnitReceipt]= [UnitReceipt]+ " & ds.Tables(0).Rows(i)("Qty") & "  where UnitPO ='" & ds.Tables(0).Rows(i)("PONumber") & "'  and ReceiptNumber ='" & ReceiptNumber & "' and PartNo ='" & Trim(ds.Tables(0).Rows(i)("PartNo")) & "'"
    '                            If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
    '                                MessageBox.Show("chua Cap nhat da Delivery_sp . tam ngung di")
    '                                Exit Function
    '                            End If
    '                        Else
    '                            MySQLStr = "INSERT INTO [Receipt_sp]([ReceiptNumber],[POnumber],[DateExpected],[PartNo],[UnitPO],[UnitOrder],[UnitReceipt],[POnumberLines])" _
    '                          & " VALUES ('" & ReceiptNumber & "','" & Trim(ds2.Tables(0).Rows(0)("PONumber")) & "','" & Format(Now(), "yyyy-MMM-dd") & "','" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "'," & ds.Tables(0).Rows(i)("Qty") & "," & ds.Tables(0).Rows(i)("Qty") & "," & ds.Tables(0).Rows(i)("Qty") & ",'1')"
    '                            If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
    '                                MessageBox.Show("chua Cap nhat Receipt_sp. tam ngung di")
    '                                Exit Function
    '                            End If
    '                        End If

    '                        MySQLStr = "UPDATE WH_PDReturnMaterial SET PtintStatus ='2', ReceiptNote = '" & ReceiptNumber & "', PO= '" & Trim(ds2.Tables(0).Rows(0)("PONumber")) & "', PartName ='" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "'where ID ='" & ds.Tables(0).Rows(i)("ID") & "'"
    '                        If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = False Then
    '                            MessageBox.Show("chua Cap nhat da in nhan. tam ngung di")
    '                            Exit Function
    '                        End If

    '                    Else
    '                        MessageBox.Show("BarcodePrinter > 2")
    '                    End If
    '                Else
    '                    MessageBox.Show(" tbInventory > 2")
    '                End If
    '            Next

    '            '    MySQLStr = "SELECT PartName, PO, sum(Qty) as Qty, Code, ReceiptNote  FROM WH_PDReturnMaterial GROUP BY PartName, PO,  Code, HAVING PtintStatus = '2' ORDER BY PartName"

    '            MySQLStr = "SELECT PartName, PO, Sum(Qty) AS Qty, Code, PtintStatus, ReceiptNote FROM WH_PDReturnMaterial GROUP BY PartName, PO, Code, PtintStatus, ReceiptNote HAVING PtintStatus = '2' AND ReceiptNote = '" & ReceiptNumber & "'"
    '            'MySQLStr = "SELECT PartName, PO, Sum(Qty) AS Qty, Code, PtintStatus, ReceiptNote FROM WH_PDReturnMaterial GROUP BY PartName, PO, Code, PtintStatus, ReceiptNote HAVING PtintStatus)= '2' AND ReceiptNote = '" & ReceiptNumber & "'"
    '            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
    '            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
    '                dsSubObj.MainTable.Clear()
    '                For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
    '                    MyResult = True
    '                    GetWHDES(Trim(ds.Tables(0).Rows(i)("PartName")), MyPDes, MyPUnit)
    '                    dsSubObj.MainTable.AddMainTableRow(i + 1, ds.Tables(0).Rows(i)("PartName"), "", ds.Tables(0).Rows(i)("PO"), MyPUnit, "PD return Material", ds.Tables(0).Rows(i)("Qty"), ds.Tables(0).Rows(i)("Qty"), "", CurrentUserName, "WH_01", "", "", ds.Tables(0).Rows(i)("ReceiptNote"))
    '                Next
    '            End If


    '        Else
    '            MySQLStr = "Select * from WH_PDReturnMaterial where PtintStatus ='2'  and ( PtintNote ='1' or PtintNote is null )   and Code ='" & CurrentCode & "' ORDER BY ReadingTime"
    '            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
    '            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
    '                For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
    '                    PIDRecord1 = ds.Tables(0).Rows.Count - 1
    '                    MySQLStr = "Select * from tbInventory where IDRecord =" & ds.Tables(0).Rows(i)("IDRecord") & ""
    '                    ds1 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
    '                    If ds1.Tables.Count <> 0 AndAlso ds1.Tables(0).Rows.Count = 1 Then
    '                        MySQLStr = "Select * from BarcodePrinter where IDRecord = " & ds.Tables(0).Rows(i)("IDRecord") & ""
    '                        ds2 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
    '                        If ds2.Tables.Count <> 0 AndAlso ds2.Tables(0).Rows.Count = 1 Then
    '                            MyResult = True
    '                            dsObj.MainTable.AddMainTableRow(Trim(ds2.Tables(0).Rows(0)("IDRecord")), "*ID" & Trim(ds2.Tables(0).Rows(0)("IDRecord")) & "*", Trim(ds2.Tables(0).Rows(0)("PartNo")), "*" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "*", Trim(ds.Tables(0).Rows(i)("Shelf")), CInt(Trim(ds1.Tables(0).Rows(0)("Inventory"))), "*" & CInt(Trim(ds1.Tables(0).Rows(0)("Inventory"))) & "*", Trim(ds2.Tables(0).Rows(0)("PONumber")), "*" & Trim(ds2.Tables(0).Rows(0)("PONumber")) & "*", Trim(ds2.Tables(0).Rows(0)("ReceiptDate")), CInt(Trim(ds2.Tables(0).Rows(0)("TotalQty"))), Trim(ds.Tables(0).Rows(i)("Code")), "", "")
    '                        Else
    '                            MessageBox.Show("BarcodePrinter > 2")
    '                        End If
    '                    Else
    '                        MessageBox.Show(" tbInventory > 2")
    '                    End If
    '                Next
    '                MySQLStr = "SELECT PartName, PO, Sum(Qty) AS Qty, Code, PtintStatus, ReceiptNote FROM WH_PDReturnMaterial GROUP BY PartName, PO, Code, PtintStatus, ReceiptNote HAVING PtintStatus = '2' AND ReceiptNote = '" & ds.Tables(0).Rows(0)("ReceiptNote") & "'"
    '                ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
    '                If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
    '                    dsSubObj.MainTable.Clear()
    '                    For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
    '                        MyResult = True
    '                        GetWHDES(Trim(ds.Tables(0).Rows(i)("PartName")), MyPDes, MyPUnit)
    '                        dsSubObj.MainTable.AddMainTableRow(i + 1, ds.Tables(0).Rows(i)("PartName"), "", ds.Tables(0).Rows(i)("PO"), MyPUnit, "PD return Material", ds.Tables(0).Rows(i)("Qty"), ds.Tables(0).Rows(i)("Qty"), "", CurrentUserName, "WH_01", "", "", ds.Tables(0).Rows(i)("ReceiptNote"))
    '                    Next
    '                End If

    '            End If
    '        End If
    '    Catch ex As Exception
    '        MessageBox.Show("Loi roi ban oi")
    '        ErrRecording("", "frmMain/PDReturnMaterial", ex.Message)
    '        MyResult = False
    '    End Try
    '    Return MyResult
    'End Function




    'Private Function PDReturnMaterial1() As Boolean
    '    Dim MyResult As Boolean = False
    '    Dim MySQLStr As String = ""
    '    Dim ds As DataSet
    '    Dim ds1 As DataSet
    '    Dim ds2 As DataSet
    '    Dim ds3 As DataSet
    '    Dim ds4 As DataSet
    '    Dim ReceiptNumber As String = ""
    '    Try
    '        MySQLStr = "Select * from WH_PDReturnMaterial where PtintStatus ='1' and Code ='" & CurrentCode & "' ORDER BY ReadingTime"
    '        ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
    '        If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
    '            PIDRecord1 = ds.Tables(0).Rows.Count - 1
    '            For i As Integer = 0 To ds.Tables(0).Rows.Count - 1

    '                MySQLStr = "Select * from tbInventory where IDRecord =" & ds.Tables(0).Rows(i)("IDRecord") & ""
    '                ds1 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
    '                If ds1.Tables.Count <> 0 AndAlso ds1.Tables(0).Rows.Count = 1 Then
    '                    MySQLStr = "Select * from BarcodePrinter where IDRecord = " & ds.Tables(0).Rows(i)("IDRecord") & ""
    '                    ds2 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
    '                    If ds2.Tables.Count <> 0 AndAlso ds2.Tables(0).Rows.Count = 1 Then
    '                        MyResult = True
    '                        dsObj.MainTable.AddMainTableRow(Trim(ds2.Tables(0).Rows(0)("IDRecord")), "*ID" & Trim(ds2.Tables(0).Rows(0)("IDRecord")) & "*", Trim(ds2.Tables(0).Rows(0)("PartNo")), "*" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "*", Trim(ds.Tables(0).Rows(i)("Shelf")), CInt(Trim(ds1.Tables(0).Rows(0)("Inventory"))) + CInt(Trim(ds.Tables(0).Rows(i)("Qty"))), "*" & CInt(Trim(ds1.Tables(0).Rows(0)("Inventory"))) + CInt(Trim(ds.Tables(0).Rows(i)("Qty"))) & "*", Trim(ds2.Tables(0).Rows(0)("PONumber")), "*" & Trim(ds2.Tables(0).Rows(0)("PONumber")) & "*", Trim(ds2.Tables(0).Rows(0)("ReceiptDate")), CInt(Trim(ds2.Tables(0).Rows(0)("TotalQty"))), Trim(ds.Tables(0).Rows(i)("Code")), "", "")


    '                        Dim MyID As String = Format(Now(), "yyMMddhhmmss")
    '                        Dim ID As String = 99999 & "- " & Trim(ds2.Tables(0).Rows(0)("PartNo"))
    '                        MySQLStr = "SELECT PartNo, Sum(ActWIP) AS SumOfActWIP FROM  PDRemainQty GROUP BY PDRemainQty.PartNo HAVING PartNo ='" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & " '  and Sum(ActWIP) > 0 "
    '                        'MySQLStr = "SELECT * FROM PDRemainQty where PartNo ='" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "' and  ActWIP > 0"
    '                        Dim dspd = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
    '                        If dspd.Tables.Count <> 0 AndAlso dspd.Tables(0).Rows.Count > 0 Then
    '                            MySQLStr = "SELECT * FROM PDRemainQty where ID ='" & ID & "'"
    '                            Dim dsp = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
    '                            If dsp.Tables.Count <> 0 AndAlso dsp.Tables(0).Rows.Count = 0 Then
    '                                MySQLStr = "INSERT INTO [PDRemainQty]([ID],[StockNo],[PartNo],[WO],[WQty],[Delivery],[ActWIP],[UpdateTime])" _
    '                                & " VALUES ('" & MyID & i & "','" & StockNo & "','" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "','99999',99999," & CInt(Trim(ds.Tables(0).Rows(i)("Qty"))) & ",'" & -CInt(Trim(ds.Tables(0).Rows(i)("Qty"))) & "','" & Now() & "')"
    '                                If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = True Then
    '                                    MyResult = True
    '                                Else
    '                                    MessageBox.Show("loi")
    '                                End If
    '                            End If
    '                        End If
    '                        MySQLStr = "UPDATE tbInventory SET Location = '" & ds.Tables(0).Rows(i)("Shelf") & "',Delivery = " & CInt(ds1.Tables(0).Rows(0)("Delivery")) - CInt(ds.Tables(0).Rows(i)("Qty")) & " WHERE IDRecord='" & ds.Tables(0).Rows(i)("IDRecord") & "'"
    '                        If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
    '                            MessageBox.Show("chua Cap nhat shelf. tam ngung di")
    '                            Exit Function
    '                        End If
    '                        MySQLStr = "UPDATE IDandLocation SET Location ='" & ds.Tables(0).Rows(i)("Shelf") & "'where ID ='" & ds.Tables(0).Rows(i)("IDRecord") & "'"
    '                        If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
    '                            MessageBox.Show("chua Cap nhat shelf. tam ngung di")
    '                            Exit Function
    '                        End If
    '                        MySQLStr = "UPDATE LOCATION SET LocationID = '" & ds.Tables(0).Rows(i)("Shelf") & "' WHERE IDRECORD='" & ds.Tables(0).Rows(i)("IDRecord") & "'"
    '                        If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
    '                            MessageBox.Show("chua Cap nhat shelf. tam ngung di")
    '                            Exit Function
    '                        End If

    '                        MySQLStr = "Select * from Receipt_Barcode where IDRecord =" & ds.Tables(0).Rows(i)("IDRecord") & ""
    '                        ds3 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
    '                        If ds3.Tables.Count <> 0 AndAlso ds3.Tables(0).Rows.Count = 1 Then
    '                            MySQLStr = "UPDATE Receipt_Barcode SET Quantity = Quantity + " & ds.Tables(0).Rows(i)("Qty") & ", OldQty = '" & ds3.Tables(0).Rows(0)("Quantity") & "-" & ds.Tables(0).Rows(i)("Qty") & "' WHERE IDRECORD='" & ds.Tables(0).Rows(i)("IDRecord") & "'"
    '                            If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
    '                                MessageBox.Show("chua Cap nhat Receipt_Barcode. tam ngung di")
    '                                Exit Function
    '                            End If
    '                        End If
    '                        ReceiptNumber = "PDRM " & Format(Now(), "yyMM")
    '                        MySQLStr = "Select * from Receipt where substring(ReceiptNumber,1,9) = '" & ReceiptNumber & "' order by ReceiptNumber desc"
    '                        ds4 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
    '                        If ds4.Tables.Count <> 0 AndAlso ds4.Tables(0).Rows.Count > 0 Then
    '                            ReceiptNumber = ReceiptNumber & " " & (CInt(Mid(ds4.Tables(0).Rows(0)("ReceiptNumber"), 11, 3)) + 1).ToString("000")
    '                        Else
    '                            ReceiptNumber = ReceiptNumber & " 001"
    '                        End If

    '                        MySQLStr = "INSERT INTO [Receipt]([TransactionNumber],[ReceiptNumber],[StockNo],[SupplierNumber],[Receiver],[Deliverer],[R_Date],[MonthCurr],[lock],[bar_Lock],[SysDate],[Ck_Status])" _
    '                        & " VALUES ('PDRM','" & ReceiptNumber & "','" & StockNo & "','PDRM','0004','0001','" & Format(Now(), "yyyy-MMM-dd") & "','" & Format(Now(), "MM/yyyy") & "',0,3,'" & Format(Now(), "yyyy-MMM-dd") & "',1)"
    '                        If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
    '                            MessageBox.Show("chua Cap nhat Receipt. tam ngung di")
    '                            Exit Function
    '                        End If

    '                        MySQLStr = "INSERT INTO [Receipt_sp]([ReceiptNumber],[POnumber],[DateExpected],[PartNo],[UnitPO],[UnitOrder],[UnitReceipt],[POnumberLines])" _
    '                      & " VALUES ('" & ReceiptNumber & "','" & Trim(ds2.Tables(0).Rows(0)("PONumber")) & "','" & Format(Now(), "yyyy-MMM-dd") & "','" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "'," & ds.Tables(0).Rows(i)("Qty") & "," & ds.Tables(0).Rows(i)("Qty") & "," & ds.Tables(0).Rows(i)("Qty") & ",'1')"
    '                        If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
    '                            MessageBox.Show("chua Cap nhat Receipt_sp. tam ngung di")
    '                            Exit Function
    '                        End If

    '                        MySQLStr = "UPDATE WH_PDReturnMaterial SET PtintStatus ='2'where ID ='" & ds.Tables(0).Rows(i)("ID") & "'"
    '                        If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = False Then
    '                            MessageBox.Show("chua Cap nhat da in nhan. tam ngung di")
    '                            Exit Function
    '                        End If

    '                    Else
    '                        MessageBox.Show("BarcodePrinter > 2")
    '                    End If
    '                Else
    '                    MessageBox.Show(" tbInventory > 2")
    '                End If
    '            Next
    '        Else
    '            MySQLStr = "Select * from WH_PDReturnMaterial where PtintStatus ='2'  and ( PtintNote ='1' or PtintNote is null )   and Code ='" & CurrentCode & "' ORDER BY ReadingTime"
    '            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
    '            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
    '                For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
    '                    PIDRecord1 = ds.Tables(0).Rows.Count - 1
    '                    MySQLStr = "Select * from tbInventory where IDRecord =" & ds.Tables(0).Rows(i)("IDRecord") & ""
    '                    ds1 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
    '                    If ds1.Tables.Count <> 0 AndAlso ds1.Tables(0).Rows.Count = 1 Then
    '                        MySQLStr = "Select * from BarcodePrinter where IDRecord = " & ds.Tables(0).Rows(i)("IDRecord") & ""
    '                        ds2 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
    '                        If ds2.Tables.Count <> 0 AndAlso ds2.Tables(0).Rows.Count = 1 Then
    '                            MyResult = True
    '                            dsObj.MainTable.AddMainTableRow(Trim(ds2.Tables(0).Rows(0)("IDRecord")), "*ID" & Trim(ds2.Tables(0).Rows(0)("IDRecord")) & "*", Trim(ds2.Tables(0).Rows(0)("PartNo")), "*" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "*", Trim(ds.Tables(0).Rows(i)("Shelf")), CInt(Trim(ds1.Tables(0).Rows(0)("Inventory"))), "*" & CInt(Trim(ds1.Tables(0).Rows(0)("Inventory"))) & "*", Trim(ds2.Tables(0).Rows(0)("PONumber")), "*" & Trim(ds2.Tables(0).Rows(0)("PONumber")) & "*", Trim(ds2.Tables(0).Rows(0)("ReceiptDate")), CInt(Trim(ds2.Tables(0).Rows(0)("TotalQty"))), Trim(ds.Tables(0).Rows(i)("Code")), "", "")
    '                        Else
    '                            MessageBox.Show("BarcodePrinter > 2")
    '                        End If
    '                    Else
    '                        MessageBox.Show(" tbInventory > 2")
    '                    End If
    '                Next
    '            End If
    '        End If
    '    Catch ex As Exception
    '        MessageBox.Show("Loi roi ban oi")
    '        ErrRecording("", "frmMain/PDReturnMaterial", ex.Message)
    '        MyResult = False
    '    End Try
    '    Return MyResult
    'End Function




    Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        StopP()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        dsObj.MainTable.Clear()
        If PDReturnMaterial() = True Then
            frmPDReceipteNote.Show()
            frmPDReturnMaterial.Show()
        Else
            MessageBox.Show("KHÔNG CÓ DỮ LIỆU IN ĐÂU. XEM LẠI BẠN NHẬP CHƯA HAY IN ROI CHƯA")
        End If
    End Sub

    Private Sub DeliveryPDToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeliveryPDToolStripMenuItem.Click
        DeliveryFlg = 1
        frmDeliveryMaterial.Show()
    End Sub

    Private Sub SubPartToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SubPartToolStripMenuItem.Click
        frmSubPart.Show()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim RQtyFlg As Boolean = False
        If RdDNote.Checked = False OrElse Trim(Me.CmdLOC.Text) = "" Then
            MessageBox.Show("Yêu câu nhập phiếu lấy Part")
        Else
            MyGetPartNote = UCase(Trim(Me.CmdLOC.Text))
            dsObj.MainTable.Clear()
            If DeliverymaterialPD(RQtyFlg) = True Then
                frmIDDelivery.Show()
                If RQtyFlg = True Then
                    MessageBox.Show("CÓ NHÃN TỒN CỦA NGUYÊN VẬT LIỆU. CHÚ Ý, CHÚ Ý NHA")
                    frmIDRemain.Show()
                End If
            Else
                MessageBox.Show("KHÔNG CÓ DỮ LIỆU IN ĐÂU. XEM LẠI BẠN NHẬP CHƯA HAY IN ROI CHƯA")
            End If
        End If
    End Sub

    Private Function DeliverymaterialPD(ByRef RQtyFlg As Boolean) As Boolean
        Dim MyResult As Boolean = False
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Dim ds1 As DataSet
        Dim ds2 As DataSet
        Dim ds3 As DataSet
        Dim dsC As DataSet
        Dim dsMID As DataSet
        Dim dsCSSs As DataSet
        Dim dsSUBID As DataSet
        Dim MyIDDelivery As String = ""
        Dim ReceiptNumber As String = ""
        Dim SubIDR As String = ""
        Dim MyQty As String = ""
        Dim MyRQty As String = ""
        Dim SN As String = ""
        Dim UpID As String = ""
        Dim ReWOQTy As Integer = 0
        Dim ReQTy As Integer = 0
        Dim RRR As Integer = 0
        Dim MID As String = ""
        Try
            MySQLStr = "Select * from WH_MaterialPicking where Marking = '2' and MarkingPDS = '2' and PrintStatus = '2' and AssignmentNote= '" & MyGetPartNote & "' and Code ='" & CurrentCode & "'"
            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                    MySQLStr = "Select * from tbInventory where IDRecord =" & ds.Tables(0).Rows(i)("IDRecord") & ""
                    ds1 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                    If ds1.Tables.Count <> 0 AndAlso ds1.Tables(0).Rows.Count = 1 Then
                        If (ds1.Tables(0).Rows(0)("SerialNumber")) Is DBNull.Value = False Then
                            SN = Trim(ds1.Tables(0).Rows(0)("SerialNumber"))
                        End If
                        MySQLStr = "Select * from BarcodePrinter where IDRecord = " & ds.Tables(0).Rows(i)("IDRecord") & ""
                        ds2 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                        If ds2.Tables.Count <> 0 AndAlso ds2.Tables(0).Rows.Count = 1 Then

                            MySQLStr = "Select * from DelBar_Sub order by IDS desc"
                            ds3 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                            If ds3.Tables.Count <> 0 AndAlso ds3.Tables(0).Rows.Count > 0 Then
                                'SubIDR = CInt(Trim(ds3.Tables(0).Rows(0)("IDS"))) + 1

                                MyQty = CInt(ds.Tables(0).Rows(i)("DeliveryQty"))
                                MyRQty = CInt(ds.Tables(0).Rows(i)("RemainQty"))

                                'If GetPartname(Trim(ds3.Tables(0).Rows(0)("IDRecord"))) = Trim(ds2.Tables(0).Rows(0)("PartNo")) Then
                                '    MyIDDelivery = Trim(ds3.Tables(0).Rows(0)("IDDelivery"))
                                'Else
                                MyIDDelivery = CInt(Trim(ds3.Tables(0).Rows(0)("IDDelivery"))) + 1
                                'End If
                                MID = ds.Tables(0).Rows(i)("ID")
                                MySQLStr = "INSERT INTO [DelBar_Sub]([IDRecord],[IDDelivery],[Quantity],[SerialNumber],[ID])" _
                                & " VALUES (" & Trim(ds2.Tables(0).Rows(0)("IDRecord")) & "," & MyIDDelivery & "," & ds.Tables(0).Rows(i)("DeliveryQty") & ",'" & SN & "','" & MID & "')"
                                If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
                                    MessageBox.Show("chua Cap nhat Receipt. tam ngung di")
                                    Exit Function
                                End If

                                MySQLStr = "INSERT INTO [Delivery_Barcode]([IDRecord],[IDDelivery],[Quantity],[SerialNumber],[Trace],[IDRecord2],[SerialNumber2])" _
                                & " VALUES (" & Trim(ds2.Tables(0).Rows(0)("IDRecord")) & "," & MyIDDelivery & "," & Trim(ds.Tables(0).Rows(i)("DeliveryQty")) & ",'" & SN & "','" & Trim(ds1.Tables(0).Rows(0)("Trace")) & "'," & Trim(ds2.Tables(0).Rows(0)("IDRecord")) & ",'" & SN & "')"
                                If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
                                    MessageBox.Show("chua Cap nhat Delivery_Barcode. tam ngung di")
                                    Exit Function
                                End If
                                Threading.Thread.Sleep(400)
                                MySQLStr = "Select *  from DelBar_Sub where ID  ='" & MID & "' order by IDS Desc "
                                dsMID = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                                If dsMID.Tables.Count <> 0 AndAlso dsMID.Tables(0).Rows.Count = 0 Then
                                    ErrRecording("", "frmMain/DeliverymaterialPD", " KO TIM THAY ID" & MID)
                                    MessageBox.Show("HE THONG KO TAO  DUOC SUBID. CAN THAN NHE. BAO CAP TREN DI. BAO ANH HUNG")
                                    Exit Function
                                Else
                                    SubIDR = dsMID.Tables(0).Rows(0)("IDs")
                                    MyIDDelivery = CInt(Trim(dsMID.Tables(0).Rows(0)("IDDelivery")))
                                End If
                                MySQLStr = "Select SubID from WH_MaterialPicking where SubID ='" & SubIDR & "' "
                                dsSUBID = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
                                If dsSUBID.Tables.Count <> 0 AndAlso dsSUBID.Tables(0).Rows.Count > 0 Then
                                    ErrRecording("", "frmMain/DeliverymaterialPD", " bi trung sub id----------------------" & SubIDR)
                                    MessageBox.Show("Chua trinh phat hien SUBID bi trung. Yeu cau thuc hien lai. Cam on nhe")
                                    Exit Function
                                End If

                                If CInt(ds1.Tables(0).Rows(0)("Delivery")) + CInt(ds.Tables(0).Rows(i)("DeliveryQty")) > CInt(ds1.Tables(0).Rows(0)("Receipt")) Then
                                    MessageBox.Show("Loi roi ban oi. Bao cap tren")
                                    ErrRecording("", "frmMain/DeliverymaterialPD", ds.Tables(0).Rows(i)("IDRecord") & "-" & CInt(ds1.Tables(0).Rows(0)("Delivery")) & "-" & CInt(ds.Tables(0).Rows(i)("DeliveryQty")) & "-" & CInt(ds1.Tables(0).Rows(0)("Receipt")))
                                    Exit Function
                                Else


                                    MySQLStr = "UPDATE tbInventory SET Delivery = " & CInt(ds1.Tables(0).Rows(0)("Delivery")) + CInt(ds.Tables(0).Rows(i)("DeliveryQty")) & " WHERE IDRecord='" & ds.Tables(0).Rows(i)("IDRecord") & "'"
                                    If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
                                        MessageBox.Show("chua Cap nhat tbInventory. tam ngung di")
                                        Exit Function
                                    End If
                                    Threading.Thread.Sleep(100)

                                    MySQLStr = "Select * from tbInventory where IDRecord =" & ds.Tables(0).Rows(i)("IDRecord") & ""
                                    dsC = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                                    If dsC.Tables.Count <> 0 AndAlso dsC.Tables(0).Rows.Count = 1 Then
                                        If dsC.Tables(0).Rows(0)("Inventory") <> ds.Tables(0).Rows(i)("RemainQty") Then
                                            ErrRecording("", "frmMain/DeliverymaterialPD", " ko bang ==============INV" & dsC.Tables(0).Rows(0)("Inventory") & "-  REMAINQTY " & ds.Tables(0).Rows(i)("RemainQty") & "-ID " & ds.Tables(0).Rows(i)("IDRecord"))
                                            MessageBox.Show("Nhan OK de tiep tuc in nhan nha ban")
                                            MySQLStr = "UPDATE tbInventory SET Delivery = " & CInt(ds1.Tables(0).Rows(0)("Delivery")) + CInt(ds.Tables(0).Rows(i)("DeliveryQty")) & " WHERE IDRecord='" & ds.Tables(0).Rows(i)("IDRecord") & "'"
                                            If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
                                                MessageBox.Show("chua Cap nhat tbInventory. tam ngung di")
                                                Exit Function
                                            End If
                                        End If
                                    End If
                                End If
                                MyResult = True
                                ReWOQTy = GetQtyOfPart(Trim(ds.Tables(0).Rows(i)("wo")), Trim(ds2.Tables(0).Rows(0)("PartNo")), UpID)
                                ReQTy = MyQty - ReWOQTy
                                ' ReQTy = ReQTy - ReQTy * 0.02
                                RRR = CInt(ReQTy) * 0.02
                                ReQTy = ReQTy - RRR


                                'dsObj.MainTable.AddMainTableRow(Trim(ds2.Tables(0).Rows(0)("IDRecord")), "*ID" & Trim(ds2.Tables(0).Rows(0)("IDRecord")) & "*", Trim(ds2.Tables(0).Rows(0)("PartNo")), "*" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "*", Trim(ds.Tables(0).Rows(i)("Shelf")), CInt(Trim(ds1.Tables(0).Rows(0)("Inventory"))) + CInt(Trim(ds.Tables(0).Rows(i)("Qty"))), "*" & CInt(Trim(ds1.Tables(0).Rows(0)("Inventory"))) + CInt(Trim(ds.Tables(0).Rows(i)("Qty"))) & "*", Trim(ds2.Tables(0).Rows(0)("PONumber")), "*" & Trim(ds2.Tables(0).Rows(0)("PONumber")) & "*", Trim(ds2.Tables(0).Rows(0)("ReceiptDate")), CInt(Trim(ds2.Tables(0).Rows(0)("TotalQty"))), Trim(ds.Tables(0).Rows(i)("Code")), "", "")

                                MySQLStr = "UPDATE WH_MaterialPicking SET Marking ='3',UnitWO = " & ReQTy & ", [SubID] ='" & SubIDR & "'where ID ='" & ds.Tables(0).Rows(i)("ID") & "'"
                                If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = False Then
                                    MessageBox.Show("chua Cap nhat da in nhan WH_MaterialPicking . tam ngung di")
                                    Exit Function
                                Else
                                    Threading.Thread.Sleep(500)
                                    MySQLStr = "Select * from WH_MaterialPicking where Marking ='3' and ID ='" & ds.Tables(0).Rows(i)("ID") & "'"
                                    dsCSSs = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
                                    If dsCSSs.Tables.Count <> 0 AndAlso dsCSSs.Tables(0).Rows.Count = 1 Then
                                        If ReQTy < 0 Then ReQTy = 0
                                        dsObj.MainTable.AddMainTableRow(Trim(ds2.Tables(0).Rows(0)("IDRecord")), "*ID" & Trim(ds2.Tables(0).Rows(0)("IDRecord")) & "*", Trim(ds2.Tables(0).Rows(0)("PartNo")), "*" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "*", Trim(ds.Tables(0).Rows(i)("PDShelf")), MyQty & "   (" & ReQTy & ")", SN, Trim(ds2.Tables(0).Rows(0)("PONumber")), dsCSSs.Tables(0).Rows(0)("SubID"), Trim(ds2.Tables(0).Rows(0)("ReceiptDate")), (Trim(ds2.Tables(0).Rows(0)("TotalQty"))), Trim(ds.Tables(0).Rows(i)("wo")), "*" & SN & "*", "*" & dsCSSs.Tables(0).Rows(0)("SubID") & "*")

                                        If MyRQty > 0 Then
                                            dsSubObj.MainTable.AddMainTableRow(Trim(ds2.Tables(0).Rows(0)("IDRecord")), "*ID" & Trim(ds2.Tables(0).Rows(0)("IDRecord")) & "*", Trim(ds2.Tables(0).Rows(0)("PartNo")), "*" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "*", Trim(ds.Tables(0).Rows(i)("Shelf")), MyRQty, SN, Trim(ds2.Tables(0).Rows(0)("PONumber")), dsCSSs.Tables(0).Rows(0)("SubID"), Trim(ds2.Tables(0).Rows(0)("ReceiptDate")), (Trim(ds2.Tables(0).Rows(0)("TotalQty"))), Trim(ds.Tables(0).Rows(i)("wo")), "*" & SN & "*", "*" & dsCSSs.Tables(0).Rows(0)("SubID") & "*")
                                            RQtyFlg = True
                                        End If
                                    Else
                                        MessageBox.Show("Bao a Hung")
                                    End If
                                End If

                                MySQLStr = "UPDATE BOM SET Qty = " & ReWOQTy - MyQty & " where ID ='" & UpID & "'"
                                If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = False Then
                                    MessageBox.Show("chua Cap nhat da bom . tam ngung di")
                                    Exit Function
                                End If

                            End If
                            'MySQLStr = "UPDATE WH_MaterialPicking SET Marking ='3',[DeliveryNote] ='" & ReceiptNumber & "'where ID ='" & ds.Tables(0).Rows(i)("ID") & "'"
                        Else
                            MessageBox.Show("BarcodePrinter > 2")
                        End If
                    Else
                        MessageBox.Show(" tbInventory > 2")
                    End If

                Next
            Else
                Dim ReQtyD As String = ""
                MySQLStr = "Select * from WH_MaterialPicking where Marking = '3' and PrintStatus = '2' and AssignmentNote= '" & MyGetPartNote & "' and Code ='" & CurrentCode & "' and PrintNote ='1'"
                ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
                If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                    For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                        MySQLStr = "Select * from tbInventory where IDRecord =" & ds.Tables(0).Rows(i)("IDRecord") & ""
                        ds1 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                        If ds1.Tables.Count <> 0 AndAlso ds1.Tables(0).Rows.Count = 1 Then
                            MySQLStr = "Select * from BarcodePrinter where IDRecord = " & ds.Tables(0).Rows(i)("IDRecord") & ""
                            ds2 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                            If ds2.Tables.Count <> 0 AndAlso ds2.Tables(0).Rows.Count = 1 Then
                                SubIDR = CInt(ds.Tables(0).Rows(i)("SubID"))
                                MyQty = CInt(ds.Tables(0).Rows(i)("DeliveryQty"))
                                MyRQty = CInt(ds.Tables(0).Rows(i)("RemainQty"))
                                ReQtyD = IIf(ds.Tables(0).Rows(i)("UnitWO") Is DBNull.Value, " ", (ds.Tables(0).Rows(i)("UnitWO")))
                                RRR = 0
                                If ReQtyD Is DBNull.Value = False AndAlso IsNumeric(ReQtyD) = True Then
                                    RRR = CInt(ReQtyD) * 0.02
                                    ReQtyD = ReQtyD - RRR
                                Else
                                    ReQtyD = 0
                                End If
                                ' ReQtyD = ReQtyD - ReQtyD * 0.02
                                MyResult = True
                                'if sn
                                dsObj.MainTable.AddMainTableRow(Trim(ds2.Tables(0).Rows(0)("IDRecord")), "*ID" & Trim(ds2.Tables(0).Rows(0)("IDRecord")) & "*", Trim(ds2.Tables(0).Rows(0)("PartNo")), "*" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "*", Trim(ds.Tables(0).Rows(i)("PDShelf")), MyQty & "   (" & ReQtyD & ")", SN, Trim(ds2.Tables(0).Rows(0)("PONumber")), SubIDR, Trim(ds2.Tables(0).Rows(0)("ReceiptDate")), (Trim(ds2.Tables(0).Rows(0)("TotalQty"))), Trim(ds.Tables(0).Rows(i)("wo")), "*" & SN & "*", "*" & SubIDR & "*")
                                If MyRQty > 0 Then
                                    dsSubObj.MainTable.AddMainTableRow(Trim(ds2.Tables(0).Rows(0)("IDRecord")), "*ID" & Trim(ds2.Tables(0).Rows(0)("IDRecord")) & "*", Trim(ds2.Tables(0).Rows(0)("PartNo")), "*" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "*", Trim(ds.Tables(0).Rows(i)("Shelf")), MyRQty, SN, Trim(ds2.Tables(0).Rows(0)("PONumber")), SubIDR, Trim(ds2.Tables(0).Rows(0)("ReceiptDate")), (Trim(ds2.Tables(0).Rows(0)("TotalQty"))), Trim(ds.Tables(0).Rows(i)("wo")), "*" & SN & "*", "*" & SubIDR & "*")
                                    RQtyFlg = True
                                End If
                            Else
                                MessageBox.Show("BarcodePrinter > 2")
                            End If
                        Else
                            MessageBox.Show(" tbInventory > 2")
                        End If
                    Next
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString())
            ErrRecording("", "frmMain/DeliverymaterialPD", ex.Message)
            MyResult = False
        End Try
        Return MyResult
    End Function

    Private Function GetQtyOfPart(ByVal Myww As String, ByVal Mypp As String, ByRef UID As String) As String
        Dim MyResult As String = 0
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Try
            MySQLStr = "SELECT * FROM BOM where wono ='" & Myww & "' and item='" & Mypp & "'"
            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count = 1 Then
                MyResult = IIf(ds.Tables(0).Rows(0)("Qty") Is DBNull.Value, ds.Tables(0).Rows(0)("oqty"), ds.Tables(0).Rows(0)("Qty"))
                UID = ds.Tables(0).Rows(0)("ID")
            Else
                MyResult = 0
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/GetQtyOfPart", ex.Message)
            MessageBox.Show("LOI. BAO CAP TREN")
        Finally
        End Try
        Return MyResult
    End Function

    Private Function IDDeliveryMPD(ByVal IDDM As String) As Boolean
        Dim MyResult As Boolean = False
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Dim ds1 As DataSet
        Dim ds2 As DataSet
        'Dim ds3 As DataSet
        Dim MyIDDelivery As String = ""
        Dim ReceiptNumber As String = ""
        Dim SubIDR As String = ""
        Dim MyQty As String = ""
        Dim SN As String = ""
        Dim ReQtyD As String = ""
        Dim RRR As Integer = 0
        Try
            MySQLStr = "Select * from WH_MaterialPicking where ( Marking ='3' or  Marking ='4')  and PrintStatus = '2' and ID = '" & IDDM & "' "
            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                dsObj.MainTable.Clear()
                For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                    MySQLStr = "Select * from tbInventory where IDRecord =" & ds.Tables(0).Rows(i)("IDRecord") & ""
                    ds1 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                    If ds1.Tables.Count <> 0 AndAlso ds1.Tables(0).Rows.Count = 1 Then
                        If (ds1.Tables(0).Rows(0)("SerialNumber")) Is DBNull.Value = False Then
                            SN = Trim(ds1.Tables(0).Rows(0)("SerialNumber"))
                        End If
                        MySQLStr = "Select * from BarcodePrinter where IDRecord = " & ds.Tables(0).Rows(i)("IDRecord") & ""
                        ds2 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                        If ds2.Tables.Count <> 0 AndAlso ds2.Tables(0).Rows.Count = 1 Then
                            'MySQLStr = "Select * from DelBar_Sub order by IDDelivery desc"
                            'ds3 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                            'If ds3.Tables.Count <> 0 AndAlso ds3.Tables(0).Rows.Count > 0 Then
                            'SubIDR = CInt(Trim(ds3.Tables(0).Rows(0)("IDS"))) + 1
                            SubIDR = CInt(Trim(ds.Tables(0).Rows(0)("SubID")))
                            MyQty = CInt(ds.Tables(0).Rows(i)("DeliveryQty"))
                            ReQtyD = IIf(ds.Tables(0).Rows(i)("UnitWO") Is DBNull.Value, " ", (ds.Tables(0).Rows(i)("UnitWO")))
                            RRR = 0
                            If ReQtyD Is DBNull.Value = False AndAlso IsNumeric(ReQtyD) = True Then
                                RRR = CInt(ReQtyD) * 0.02
                                ReQtyD = ReQtyD - RRR
                            Else
                                ReQtyD = 0
                            End If
                            'MyIDDelivery = Trim(ds3.Tables(0).Rows(0)("IDDelivery"))
                            MyResult = True
                            dsObj.MainTable.AddMainTableRow(Trim(ds2.Tables(0).Rows(0)("IDRecord")), "*ID" & Trim(ds2.Tables(0).Rows(0)("IDRecord")) & "*", Trim(ds2.Tables(0).Rows(0)("PartNo")), "*" & Trim(ds2.Tables(0).Rows(0)("PartNo")) & "*", Trim(ds.Tables(0).Rows(i)("PDShelf")), MyQty & "   (" & ReQtyD & ")", SN, Trim(ds2.Tables(0).Rows(0)("PONumber")), SubIDR, Trim(ds2.Tables(0).Rows(0)("ReceiptDate")), (Trim(ds2.Tables(0).Rows(0)("TotalQty"))), Trim(ds.Tables(0).Rows(i)("wo")), "*" & SN & "*", "*" & SubIDR & "*")
                            'End If

                        Else
                            MessageBox.Show("BarcodePrinter > 2")
                        End If
                    Else
                        MessageBox.Show(" tbInventory > 2")
                    End If
                Next
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString())
            ErrRecording("", "frmMain/IDDeliveryMPD", ex.Message)
            MyResult = False
        End Try
        Return MyResult
    End Function

    Private Function GetPartname(ByVal MyIDR As String) As String
        Dim MyResult As String = ""
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Try
            MySQLStr = "SELECT * from tbinventory where IDRecord ='" & MyIDR & "'"
            ds = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count = 1 Then
                MyResult = ds.Tables(0).Rows(0)("PartNo")
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/GetPartname", ex.Message)
        End Try
        Return MyResult
    End Function

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        frmWO.Show()
        'Dim MyCaption As String = "HO TRO!"
        'If DeliveryMPDNote() = True Then
        '    frmDeliveryMNote.Show()
        'Else
        '    Dim MyMessage As String = ("CHÀO BẠN. BẠN ĐÃ IN PHIẾU XUẤT KHO RỒI. BẠN MUỐN IN PHIẾU XUẤT KHO CUỐI CÙNG KHÔNG HỈ ?")
        '    Dim Result As String = MessageBox.Show(MyMessage, MyCaption, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
        '    If Result = Windows.Forms.DialogResult.OK Then
        '        If DeliveryLMPDNote() = True Then
        '            frmDeliveryMNote.Show()
        '        Else
        '            MessageBox.Show("CŨNG KHÔNG IN ĐƯỢC BẠN ƠI. BÁO CẤP TRÊN")
        '        End If
        '    ElseIf Result = Windows.Forms.DialogResult.Cancel Then

        '    End If
        'End If
    End Sub

    Private Function VDeliveryMPDNote(ByVal ReceiptNumber As String) As Boolean
        Dim MyResult As Boolean = False
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Dim MyPDes As String = ""
        Dim MyPUnit As String = ""
        Try

            MySQLStr = "SELECT PartName, WO, sum(DeliveryQty) as Qty, LeadCode, Marking, StockNo,DeliveryNote  FROM WH_MaterialPicking GROUP BY PartName, WO,  LeadCode, Marking, StockNo,DeliveryNote HAVING WH_MaterialPicking.Marking = '4'  and DeliveryNote ='" & ReceiptNumber & "' ORDER BY PartName"
            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                dsObj.MainTable.Clear()
                For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                    MyResult = True
                    GetWHDES(Trim(ds.Tables(0).Rows(i)("PartName")), MyPDes, MyPUnit)
                    dsObj.MainTable.AddMainTableRow(i + 1, MyPDes, ds.Tables(0).Rows(i)("StockNo"), ds.Tables(0).Rows(i)("PartName"), MyPUnit, ds.Tables(0).Rows(i)("wo"), ds.Tables(0).Rows(i)("Qty"), ds.Tables(0).Rows(i)("Qty"), 0, CurrentUserName, "", CheckStat(ds.Tables(0).Rows(i)("PartName")), ACheckResistance, ReceiptNumber)
                Next
            Else
                MySQLStr = "SELECT PartName, RMMNote, sum(Qty) as Qty, Code, Marking, StockNo,DeliveryNote  FROM WH_MaterialTransfer GROUP BY PartName, RMMNote,  Code, Marking, StockNo,DeliveryNote HAVING Marking = '2' and DeliveryNote = '" & ReceiptNumber & "'ORDER BY PartName"
                ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
                If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                    dsObj.MainTable.Clear()
                    For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                        MyResult = True
                        GetWHDES(Trim(ds.Tables(0).Rows(i)("PartName")), MyPDes, MyPUnit)
                        dsObj.MainTable.AddMainTableRow(i + 1, MyPDes, ds.Tables(0).Rows(i)("StockNo"), ds.Tables(0).Rows(i)("PartName"), MyPUnit, ds.Tables(0).Rows(i)("RMMNote"), ds.Tables(0).Rows(i)("Qty"), ds.Tables(0).Rows(i)("Qty"), 0, CurrentUserName, "", CheckStat(ds.Tables(0).Rows(i)("PartName")), ACheckResistance, ReceiptNumber)
                    Next
                    MySQLStr = "UPDATE WH_MaterialTransfer SET Marking ='2'where [DeliveryNote] ='" & ReceiptNumber & "'"
                    If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = False Then
                        MessageBox.Show("chua Cap nhat da in nhan. tam ngung di")
                        Exit Function
                    End If
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString())
            ErrRecording("", "frmMain/VDeliveryMPDNote", ex.Message)
        End Try
        Return MyResult
    End Function

    Private Function CheckStat(ByVal MyP As String) As String
        Dim MyResult As String = " "
        If ACheckResistance.Length < 10 Then ACheckResistance = " "
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Try
            MySQLStr = "Select * from QA_CheckStatResistance where ID ='" & Trim(MyP) & "'"
            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count = 1 Then
                MyResult = "Checking label"
                ACheckResistance = "NOTE:    " & "Checking label 'RESISTANCE CHECK REQUIRED LESS THAN 50 mOhm' on each box"
                MessageBox.Show("Chú ý! Phiếu xuất kho này có stat :" & MyP & ". QA phải kiểm tra điện trở. Công nhân kho phải tra QA có dán nhận diện chưa")
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString())
            ErrRecording("", "frmDeliveryMaterial/CheckStat", ex.Message)
        End Try
        Return MyResult
    End Function


    'Private Function DeliveryLMPDNote() As Boolean
    '    Dim MyResult As Boolean = False
    '    Dim MySQLStr As String = ""
    '    Dim ds As DataSet
    '    Dim ds1 As DataSet
    '    Dim MyPDes As String = ""
    '    Dim MyPUnit As String = ""
    '    Dim ReceiptNumber As String = ""
    '    Try
    '        MySQLStr = "Select DeliveryNote from WH_MaterialPicking order by DeliveryNote desc"
    '        ds1 = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
    '        If ds1.Tables.Count <> 0 AndAlso ds1.Tables(0).Rows.Count > 0 Then
    '            ReceiptNumber = ds1.Tables(0).Rows(0)("DeliveryNote")
    '        End If
    '        MySQLStr = "SELECT PartName, WO, sum(DeliveryQty) as Qty, LeadCode, Marking, StockNo,DeliveryNote  FROM WH_MaterialPicking GROUP BY PartName, WO,  LeadCode, Marking, StockNo,DeliveryNote HAVING WH_MaterialPicking.Marking = '4' AND  DeliveryNote ='" & ReceiptNumber & "'ORDER BY PartName"
    '        ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
    '        If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
    '            dsObj.MainTable.Clear()
    '            For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
    '                MyResult = True
    '                GetWHDES(Trim(ds.Tables(0).Rows(i)("PartName")), MyPDes, MyPUnit)
    '                dsObj.MainTable.AddMainTableRow(i + 1, MyPDes, ds.Tables(0).Rows(i)("StockNo"), ds.Tables(0).Rows(i)("PartName"), MyPUnit, ds.Tables(0).Rows(i)("wo"), ds.Tables(0).Rows(i)("Qty"), ds.Tables(0).Rows(i)("Qty"), 0, CurrentUserName, "", "", "", ReceiptNumber)
    '            Next
    '        End If
    '    Catch ex As Exception
    '        MessageBox.Show("Loi roi ban oi")
    '        ErrRecording("", "frmMain/DeliveryLMPDNote", ex.Message)
    '    End Try
    '    Return MyResult
    'End Function


    Private Function DeliveryMPDNote(ByVal MyWO As String) As Boolean
        Dim MyResult As Boolean = False
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Dim ds1 As DataSet
        Dim ds4 As DataSet
        Dim MyPDes As String = ""
        Dim MyPUnit As String = ""
        Dim ReceiptNumber As String = ""
        Try
            MySQLStr = "Select * from WH_MaterialPicking where Marking ='3' and wo = '" & MyWO & "' "
            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then

                ReceiptNumber = "XSX " & Format(Now(), "yyMM")
                MySQLStr = "Select * from Delivery where substring(DeliveryNumber,1,8) = '" & ReceiptNumber & "' order by DeliveryNumber desc"
                ds4 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                If ds4.Tables.Count <> 0 AndAlso ds4.Tables(0).Rows.Count > 0 Then
                    ReceiptNumber = ReceiptNumber & " " & (CInt(Mid(ds4.Tables(0).Rows(0)("DeliveryNumber"), 10, 4)) + 1).ToString("0000")
                Else
                    ReceiptNumber = ReceiptNumber & " 0001"
                End If
                MySQLStr = "INSERT INTO [Delivery]([TransactionNumber],[DeliveryNumber],[StockNo],[Receiver],[Deliverer],[R_Date],[MonthCurr])" _
                & " VALUES ('XSX','" & ReceiptNumber & "','" & StockNo & "','0004','0001','" & Format(Now(), "yyyy-MMM-dd") & "','" & Format(Now(), "MM/yyyy") & "')"
                If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
                    MessageBox.Show("chua Cap nhat Delivery. tam ngung di")
                    Exit Function
                End If

                For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                    MySQLStr = "Select * from Delivery_sp where Workorder ='" & ds.Tables(0).Rows(i)("WO") & "'  and Deliverynumber ='" & ReceiptNumber & "' and PartNo ='" & Trim(ds.Tables(0).Rows(i)("PartName")) & "'"
                    ds1 = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
                    If ds1.Tables.Count <> 0 AndAlso ds1.Tables(0).Rows.Count = 1 Then
                        MySQLStr = "UPDATE Delivery_sp SET [UnitWO] = [UnitWO]  + " & ds.Tables(0).Rows(i)("DeliveryQty") & ", [UnitDelivery]= [UnitDelivery]+ " & ds.Tables(0).Rows(i)("DeliveryQty") & ", [UnitBarcode]= [UnitBarcode]+ " & ds.Tables(0).Rows(i)("DeliveryQty") & "  where Workorder ='" & ds.Tables(0).Rows(i)("WO") & "'  and Deliverynumber ='" & ReceiptNumber & "' and PartNo ='" & Trim(ds.Tables(0).Rows(i)("PartName")) & "'"
                        If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
                            MessageBox.Show("chua Cap nhat da Delivery_sp . tam ngung di")
                            Exit Function
                        End If
                    Else
                        MySQLStr = "INSERT INTO [Delivery_sp]([DeliveryNumber],[worKorder],[PartNo],[UnitWO],[UnitDelivery],[UnitBarcode])" _
                        & " VALUES ('" & ReceiptNumber & "','" & ds.Tables(0).Rows(i)("WO") & "','" & Trim(ds.Tables(0).Rows(i)("PartName")) & "'," & ds.Tables(0).Rows(i)("DeliveryQty") & "," & ds.Tables(0).Rows(i)("DeliveryQty") & "," & ds.Tables(0).Rows(i)("DeliveryQty") & ")"
                        If MyServerConnection.ExecuteSQLSVR(MySQLStr) = False Then
                            MessageBox.Show("chua Cap nhat Receipt_sp. tam ngung di")
                            Exit Function
                        End If
                    End If
                    MySQLStr = "UPDATE WH_MaterialPicking SET [DeliveryNote] ='" & ReceiptNumber & "'where ID ='" & ds.Tables(0).Rows(i)("ID") & "'"
                    If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = False Then
                        MessageBox.Show("chua Cap nhat da in nhan. tam ngung di")
                        Exit Function
                    End If
                Next
            End If
            MySQLStr = "SELECT PartName, WO, sum(DeliveryQty) as Qty, LeadCode, Marking, StockNo,DeliveryNote  FROM WH_MaterialPicking GROUP BY PartName, WO,  LeadCode, Marking, StockNo,DeliveryNote HAVING WH_MaterialPicking.Marking = '3' and wo = '" & MyWO & "'ORDER BY PartName"
            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                dsObj.MainTable.Clear()
                For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                    MyResult = True
                    GetWHDES(Trim(ds.Tables(0).Rows(i)("PartName")), MyPDes, MyPUnit)
                    dsObj.MainTable.AddMainTableRow(i + 1, MyPDes, ds.Tables(0).Rows(i)("StockNo"), ds.Tables(0).Rows(i)("PartName"), MyPUnit, ds.Tables(0).Rows(i)("wo"), ds.Tables(0).Rows(i)("Qty"), ds.Tables(0).Rows(i)("Qty"), 0, CurrentUserName, "", "", "", ReceiptNumber)
                Next
                MySQLStr = "UPDATE WH_MaterialPicking SET Marking ='4'where [DeliveryNote] ='" & ReceiptNumber & "'"
                If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = False Then
                    MessageBox.Show("chua Cap nhat da in nhan. tam ngung di")
                    Exit Function
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString())
            ErrRecording("", "frmMain/DeliveryMPDNote", ex.Message)
        End Try
        Return MyResult
    End Function

    Private Function GetWHDES(ByVal MyPN As String, ByRef MyPDes As String, ByRef MyPUnit As String) As Boolean
        Dim MyResult As Boolean
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Try
            MySQLStr = "SELECT * FROM Products WHERE partno = '" & Trim(MyPN) & "'"
            ds = MyServerConnection.ExecuteProcedureSQLSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count = 1 Then
                MyPDes = IIf(ds.Tables(0).Rows(0)("PartName") Is DBNull.Value, MyPN, ds.Tables(0).Rows(0)("PartName"))
                MyPUnit = IIf(ds.Tables(0).Rows(0)("Unit") Is DBNull.Value, "", ds.Tables(0).Rows(0)("Unit"))
                MyResult = True
            Else
                MyPDes = MyPN
                'MessageBox.Show("KHÔNG TIM THAY TEN PART")
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/GetWHDES", ex.Message)
            MyResult = False
        Finally
        End Try
        Return MyResult
    End Function

    Private Function GetWHDES_local(ByVal MyPN As String, ByVal ARM As String, ByRef MyPDes As String, ByRef MyPUnit As String) As Boolean
        Dim MyResult As Boolean
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Try
            MySQLStr = "SELECT * FROM WH_ReceiptMaterial WHERE AReceiptNumber = '" & ARM & "' and PartNo = '" & MyPN & "'"
            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count = 1 Then
                MyPDes = IIf(ds.Tables(0).Rows(0)("Des") Is DBNull.Value, MyPN, ds.Tables(0).Rows(0)("Des"))
                MyPUnit = IIf(ds.Tables(0).Rows(0)("UnitPart") Is DBNull.Value, "", ds.Tables(0).Rows(0)("UnitPart"))
                MyResult = True
            Else
                MyResult = False
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/GetWHDES_local", ex.Message)
            MyResult = False
        Finally
        End Try
        Return MyResult
    End Function

    Private Sub OtherDeliveryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OtherDeliveryToolStripMenuItem.Click
        DeliveryFlg = 3
        frmDeliveryMaterial.Show()
    End Sub

    Private Function CheckCode(ByVal MyCode As String, ByVal MyP As String, ByRef MyCodeName As String) As Boolean
        Dim MyResult As Boolean = False
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Try
            MySQLStr = "SELECT * FROM EmployeeCode WHERE CodeID = '" & MyCode & "' and ( Position ='" & MyP & "' or  Position ='Admin' or  Position ='LeadEmployee')"
            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count = 1 Then
                MyCodeName = ds.Tables(0).Rows(0)("FirstName") & " " & ds.Tables(0).Rows(0)("MidName") & " " & ds.Tables(0).Rows(0)("LastName")
                MyResult = True
            Else
                MyResult = False
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/CheckCode", ex.Message)
            MyResult = False
        Finally
        End Try
        Return MyResult
    End Function

    Private Function SkidNo(ByVal PLN As String) As Boolean
        Dim MyResult As Boolean = False
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Try
            'MySQLStr = "SELECT * from WH_FGRDelivery where PackingNote = '" & PLN & "' order by SkidNo "
            MySQLStr = "SELECT SkidNo  FROM WH_FGRDelivery GROUP BY SkidNo, PackingNote HAVING  PackingNote = '" & PLN & "' order by SkidNo "

            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                dsObj.MainTable.Clear()
                For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                    dsObj.MainTable.AddMainTableRow(ds.Tables(0).Rows(i)("SkidNo"), "*S00" & ds.Tables(0).Rows(i)("SkidNo") & "*", UCase(PLN), UCase(CurrentUserName), "", "", "", "", "", "", "", "", "", "")
                    MyResult = True
                Next
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/CheckCode", ex.Message)
            MyResult = False
        Finally
        End Try
        Return MyResult
    End Function

    Private Function CheckCodeControler(ByVal MyCode As String, ByVal MyP As String, ByRef MyCodeName As String) As Boolean
        Dim MyResult As Boolean = False
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Try
            MySQLStr = "SELECT * FROM EmployeeCode WHERE CodeID = '" & MyCode & "' and ( Position ='Admin' or  Position ='Controler')"
            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count = 1 Then
                MyCodeName = ds.Tables(0).Rows(0)("FirstName") & " " & ds.Tables(0).Rows(0)("MidName") & " " & ds.Tables(0).Rows(0)("LastName")
                MyResult = True
            Else
                MyResult = False
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/CheckCode", ex.Message)
            MyResult = False
        Finally
        End Try
        Return MyResult
    End Function


    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        frmRequestIncoming.Show()
    End Sub

    Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint

    End Sub



    Private Sub Label9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label9.Click

    End Sub

    Private Sub ToolDeliveryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolDeliveryToolStripMenuItem.Click
        DeliveryFlg = 4
    End Sub

    Private Sub JumpIDToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles JumpIDToolStripMenuItem.Click
        frmJMP.Show()
        JumpIDFlg = 1
    End Sub

    Private Sub DeleteJumpIDToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteJumpIDToolStripMenuItem.Click
        frmJMP.Show()
        JumpIDFlg = 2
    End Sub

    Private Sub WH2DeliveryWH1ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WH2DeliveryWH1ToolStripMenuItem.Click
        FuncMS = 1
        frmInputMaterialWH2toWH1.Show()
    End Sub

    Private Sub PrintReceipteNoteFromWH2ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintReceipteNoteFromWH2ToolStripMenuItem.Click
        FuncMS = 2
        frmInputMaterialWH2toWH1.Show()
    End Sub

    Private Sub MoveStockNoteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MoveStockNoteToolStripMenuItem.Click
        frmMoveStock.Show()
    End Sub

    Private Sub InPhieuToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InPhieuToolStripMenuItem.Click
        FuncMS = 4
        frmInputMaterialWH2toWH1.Show()
    End Sub

    Private Sub WMSToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WMSToolStripMenuItem1.Click
        frmViewWHData.Show()
    End Sub

    Private Function SearchPartOfWO(ByVal MyP As String) As Boolean
        Dim MyResult As Boolean = False
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Dim PN As String = ""
        Dim PU As String = ""
        Dim MyOUTPUT As String = ""
        Me.ViewData.Rows.Clear()
        Me.ViewData.Columns("No").HeaderText = "No"
        Me.ViewData.Columns("No").Width = 30
        Me.ViewData.Columns("Customer").HeaderText = "WO"
        Me.ViewData.Columns("Customer").Width = 50
        Me.ViewData.Columns("PartN").HeaderText = "PartName"
        Me.ViewData.Columns("PartN").Width = 100
        Me.ViewData.Columns("Wordorder").HeaderText = "Qty"
        Me.ViewData.Columns("Wordorder").Width = 50

        Me.ViewData.Columns("OutQty").HeaderText = "Delivery"
        Me.ViewData.Columns("OutQty").Width = 100
        Me.ViewData.Columns("Delivery").HeaderText = "Bal"
        Me.ViewData.Columns("Delivery").Width = 60
        Me.ViewData.Columns("WIP").HeaderText = "Desc"
        Me.ViewData.Columns("WIP").Width = 200
        Me.ViewData.Columns("BAL").HeaderText = "Unit"
        Me.ViewData.Columns("BAL").Width = 60
        Me.ViewData.Columns("PDQty").HeaderText = ""
        Me.ViewData.Columns("PDQty").Width = 60
        Me.ViewData.Columns("PDRemain").HeaderText = ""
        Me.ViewData.Columns("PDRemain").Width = 60
        Me.ViewData.Columns("Comment").HeaderText = ""
        Me.ViewData.Columns("Comment").Width = 120
        Me.ViewData.Columns("PartArriveTime").HeaderText = ""
        Me.ViewData.Columns("PartArriveTime").Width = 100
        Me.ViewData.Columns("PDQty").Visible = True
        Me.ViewData.Columns("PDRemain").Visible = True
        GetWHDES(MyP, PN, PU)

        Try
            MySQLStr = " SELECT wono, item, Sum(oqty) AS Qty FROM BOM GROUP BY wono, item HAVING item  = '" & MyP & "' ORDER BY wono"
            ds = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
            If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                    MyWO = ds.Tables(0).Rows(i)("wono")
                    MyOUTPUT = GETOUTPUT(ds.Tables(0).Rows(i)("item"))
                    Me.ViewData.Rows.Add()
                    Me.ViewData.Rows(i).Cells("No").Value = i + 1
                    Me.ViewData.Rows(i).Cells("Customer").Value = ds.Tables(0).Rows(i)("wono")
                    Me.ViewData.Rows(i).Cells("PartN").Value = ds.Tables(0).Rows(i)("item")
                    Me.ViewData.Rows(i).Cells("Wordorder").Value = CInt(ds.Tables(0).Rows(i)("Qty"))
                    Me.ViewData.Rows(i).Cells("OutQty").Value = MyOUTPUT
                    Me.ViewData.Rows(i).Cells("Delivery").Value = MyOUTPUT - CInt(ds.Tables(0).Rows(i)("Qty"))
                    Me.ViewData.Rows(0).Cells("WIP").Value = PN
                    Me.ViewData.Rows(0).Cells("BAL").Value = PU

                    If MyOUTPUT - CInt(ds.Tables(0).Rows(i)("Qty")) < 0 Then
                        Me.ViewData.Rows(i).Cells("Delivery").Style.BackColor = Color.Aquamarine
                    End If
                    MyResult = False
                Next
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/SearchPartOfWO", ex.Message)
            MessageBox.Show("LOI. BAO CAP TREN")
        Finally
            If MyDBConnection IsNot Nothing Then
                MyDBConnection.DBClosing()
            End If
        End Try
        Return MyResult
    End Function

    Private Sub DRInputToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DRInputToolStripMenuItem.Click
        frmDRInput.Show()
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        dsObj.MainTable.Clear()
        If PDReturnMaterial() = True Then
            frmPDReceipteNote.Show()
            frmPDReturnMaterial.Show()
        Else
            MessageBox.Show("KHÔNG CÓ DỮ LIỆU IN ĐÂU. XEM LẠI BẠN NHẬP CHƯA HAY IN ROI CHƯA")
        End If
    End Sub

    Private Sub DPTime_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DPTime.Tick
        RMDPFlg = True
        frmRMPanel.Show()
    End Sub

    Private Sub Button5_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRMP.Click
        RMDPFlg = True
        frmRMPanel.Show()
    End Sub

    Private Sub UpdatePOFromAccpacToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdatePOFromAccpacToolStripMenuItem.Click
        DownLoadPO()
    End Sub
    Private Function DownLoadPO() As Boolean
        Dim MyResult As Boolean = False
        Dim MySQLStr As String = ""
        Dim ds As DataSet
        Dim ds1 As DataSet
        Dim vp As String = ""
        Dim vc As String = ""
        Dim pd As String = ""
        Try
            If MyDBConnection.DBConnecting = True Then
                MySQLStr = "SELECT * FROM aricst10 "
                ds = MyDBConnection.ExecuteProcedureDBSVR(MySQLStr)
                If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                    For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                        MySQLStr = "SELECT * from WH_FGCustomer where CDNumber ='" & Trim(ds.Tables(0).Rows(i)("item")) & "'"
                        ds1 = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
                        If ds1.Tables.Count <> 0 AndAlso ds1.Tables(0).Rows.Count = 0 Then
                            MySQLStr = "INSERT INTO [WH_FGCustomer]([CDNumber],[CustPart],[Cumstomer],[CDUse],[Systemtime])" _
                            & " VALUES ('" & Trim(ds.Tables(0).Rows(i)("item")) & "','" & Trim(ds.Tables(0).Rows(i)("cpartno")) & "','" & Trim(ds.Tables(0).Rows(i)("custno")) & "',1,'" & Now() & "')"
                            If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = False Then
                                MessageBox.Show("LOI")
                            End If
                        End If
                    Next
                End If

                MySQLStr = "SELECT potran10.qtyord as Qty, potran10.purno as po, potran10.vendno as vd, potran10.item as p, potran10.vpartno as vp, potran10.descrip as d, pomast10.company as vc FROM pomast10 RIGHT JOIN potran10 ON pomast10.purno = potran10.purno"
                ds = MyDBConnection.ExecuteProcedureDBSVR(MySQLStr)
                If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                    For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                        MySQLStr = "SELECT * from WH_POMaster where PO ='" & Trim(ds.Tables(0).Rows(i)("po")) & "' and  Part ='" & Trim(ds.Tables(0).Rows(i)("p")) & "' "
                        ds1 = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
                        If ds1.Tables.Count <> 0 AndAlso ds1.Tables(0).Rows.Count = 0 Then
                            If ds.Tables(0).Rows(i)("vp") Is DBNull.Value = True Then
                                vp = " "
                            Else
                                vp = Replace(Trim(ds.Tables(0).Rows(i)("vp")), "'", "*")
                            End If
                            If ds.Tables(0).Rows(i)("vc") Is DBNull.Value = True Then
                                vc = " "
                            Else
                                vc = Replace(Trim(ds.Tables(0).Rows(i)("vc")), "'", "*")
                            End If
                            If ds.Tables(0).Rows(i)("d") Is DBNull.Value = True Then
                                pd = " "
                            Else
                                pd = Replace(Trim(ds.Tables(0).Rows(i)("d")), "'", "*")
                            End If
                            MySQLStr = "INSERT INTO [WH_POMaster]([PO],[Vendno],[Part],[QtyOrd],[PartDes],[VenderPart],[Company],[SystemTime])" _
                            & " VALUES (" & Trim(ds.Tables(0).Rows(i)("po")) & ",'" & Trim(ds.Tables(0).Rows(i)("vd")) & "','" & Trim(ds.Tables(0).Rows(i)("p")) & "'," & Trim(ds.Tables(0).Rows(i)("Qty")) & ",'" & pd & "','" & vp & "','" & vc & "','" & Now() & "')"
                            If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = False Then
                                MessageBox.Show("LOI")
                            End If
                        End If
                    Next
                End If

                MySQLStr = "SELECT poyrcp10.qtyrec as Qty,poyrcp10.purno as po, poyrcp10.vendno as vd, poyrcp10.item as p, poyrcp10.vpartno as vp, poyrcp10.descrip as d, poymst10.company as vc FROM poymst10 RIGHT JOIN poyrcp10 ON poymst10.purno = poyrcp10.purno"

                'MySQLStr = "SELECT poyrcp10.purno as po, poyrcp10.vendno as vd, poyrcp10.item as p, poyrcp10.vpartno as vp, poyrcp10.descrip as d, poymst10.company as vc FROM poyrcp10 INNER JOIN poymst10 ON poyrcp10.purno = poymst10.purno "
                ds = MyDBConnection.ExecuteProcedureDBSVR(MySQLStr)
                If ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                    For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                        MySQLStr = "SELECT * from WH_POMaster where PO ='" & Trim(ds.Tables(0).Rows(i)("po")) & "' and  Part ='" & Trim(ds.Tables(0).Rows(i)("p")) & "' "
                        ds1 = MySVRTVNWConnection.ExecuteProcedureSQLTVNWSVR(MySQLStr)
                        If ds1.Tables.Count <> 0 AndAlso ds1.Tables(0).Rows.Count = 0 Then
                            If ds.Tables(0).Rows(i)("vp") Is DBNull.Value = True Then
                                vp = " "
                            Else
                                vp = Replace(Trim(ds.Tables(0).Rows(i)("vp")), "'", "*")
                            End If
                            If ds.Tables(0).Rows(i)("vc") Is DBNull.Value = True Then
                                vc = " "
                            Else
                                vc = Replace(Trim(ds.Tables(0).Rows(i)("vc")), "'", "*")
                            End If
                            If ds.Tables(0).Rows(i)("d") Is DBNull.Value = True Then
                                pd = " "
                            Else
                                pd = Replace(Trim(ds.Tables(0).Rows(i)("d")), "'", "*")
                            End If
                            MySQLStr = "INSERT INTO [WH_POMaster]([PO],[Vendno],[Part],[QtyOrd],[PartDes],[VenderPart],[Company],[SystemTime])" _
                        & " VALUES (" & Trim(ds.Tables(0).Rows(i)("po")) & ",'" & Trim(ds.Tables(0).Rows(i)("vd")) & "','" & Trim(ds.Tables(0).Rows(i)("p")) & "'," & Trim(ds.Tables(0).Rows(i)("Qty")) & ",'" & pd & "','" & vp & "','" & vc & "','" & Now() & "')"
                            If MySVRTVNWConnection.ExecuteSQLTVNWSVR(MySQLStr) = False Then
                                MessageBox.Show("LOI")
                            End If
                        End If
                    Next


                    MessageBox.Show("Download OK")
                End If
            Else
                MyResult = False
                MessageBox.Show("KHÔNG KẾT NỐI SERVER ACCPAC CỤC BỘ.KIEM TRA LAI NHA")
            End If
        Catch ex As Exception
            ErrRecording("", "frmMain/DownLoadPO", ex.Message)
            MessageBox.Show("LOI. BAO CAP TREN")
        Finally
            If MyDBConnection IsNot Nothing Then
                MyDBConnection.DBClosing()
            End If
        End Try
        Return MyResult
    End Function

    Private Sub RMAtWH1ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RMAtWH1ToolStripMenuItem.Click
        InputMaterialFlg = 1
        frmDeliveryMaterial.Show()
    End Sub

    Private Sub RMAtWH2ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RMAtWH2ToolStripMenuItem.Click
        InputMaterialFlg = 2
        frmDeliveryMaterial.Show()
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        If CheckCode(CurrentCode, "LeadEmployee", "") = True Then
            frmRMPO.Show()
        Else
            MessageBox.Show("Ban ko phai to truong kho. Dung vao day")
        End If
    End Sub

    Private Sub mnuLISTSTATTEST_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuLISTSTATTEST.Click
        If CurrentCode.Trim = "95175" Then
            frmListStatTest.Show()
        Else
            MsgBox("Bạn không được vào chức năng này.")
        End If

    End Sub




    Private Sub CmdWO_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdWO.SelectedIndexChanged

    End Sub

    Private Sub CmdLOC_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdLOC.SelectedIndexChanged

    End Sub
End Class


